【问题标题】:Proper way to share an Image (using Intents)共享图像的正确方法(使用 Intents)
【发布时间】:2013-02-13 22:45:32
【问题描述】:

我在我的应用程序中创建图像并希望共享这些社交网络 (facebook)、邮件应用程序 (gmail) 和其他可以“接收”图像的应用程序。

问题的根源(我认为)是我不想将外部存储用作图像的基础。我想使用我的数据文件夹或缓存文件夹,因为它们都不需要任何访问权限。

我用来将图像写入文件的代码(我指定了MODE_WORLD_READABLE 以便其他应用程序可以读取它们):

FileOutputStream fos = null;
try {
    fos = context.openFileOutput("image.jpg", Context.MODE_WORLD_READABLE);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} finally {
    if (fos != null)
        fos.close();
}

这是我分享图片的代码:

File internalFile = context.getFileStreamPath("image.jpg");

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(internalFile));
intent.setType("image/jpeg");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

context.startActivity(Intent.createChooser(intent, "share"));

此解决方案非常简单,适用于 facebook 等应用程序,但 不是,例如 gmail 失败:

file:// attachment paths must point to file:///mnt/sdcard

有许多“黑客”(见下文)可以让它与 gmail 一起使用,但我让我问自己是否有更好的方法来共享没有黑客的图像,我忽略了这一点。所以,对于问题:

  • 共享图像的最佳方式是什么? (外部存储?)
  • 是否还有其他(错误)行为与 gmail 类似的应用程序? (我发现 google+ 出现了一些问题)
  • 如果没有其他方法:我可以编写特殊的意图来共享到特定的应用程序吗?当用户在我的监视列表中选择应用程序时,我有默认的共享方式并覆盖它?

黑客

  1. 使用path-hack,只需将Uri 指向:

    file:///mnt/sdcard/../../my/package/name/...

    这个解决方案感觉不对。

  2. 使用ContentProvider,如here 所述。但引用自链接:

    警告:帖子中描述的方法适用于 Gmail,但显然与其他 ACTION_SEND 处理程序(例如 MMS 作曲家)存在一些问题。

    (问题:彩信作曲家崩溃)

【问题讨论】:

  • 你找到答案了吗?现在让我发疯了。
  • 不,目前我在我的应用程序中忽略了这个问题。

标签: android image android-intent share


【解决方案1】:

您尝试过 ParecelableFileDescriptor 吗?

http://developer.android.com/reference/android/os/ParcelFileDescriptor.html

创建 static ParcelFileDescriptor open(File file, int mode, Handler handler, ParcelFileDescriptor.OnCloseListener listener) 创建一个访问给定文件的新 ParcelFileDescriptor。 static ParcelFileDescriptor open(File file, int mode) Create a new ParcelFileDescriptor accessing a given file.

接收方是这样的: Returning an Input Stream from Parcel File Descriptor using Androids DownloadManager

【讨论】:

    【解决方案2】:

    你应该做 3 个步骤。
    拍照。

    public Bitmap takeScreenshot() {
        View rootView = findViewById(android.R.id.content).getRootView();
        rootView.setDrawingCacheEnabled(true);
        return rootView.getDrawingCache();
    }
    

    保存图片。

    public String saveBitmap(Bitmap bitmap) {
    
    File imagePath = new File(Environment.getExternalStorageDirectory() + “/screenshot.png”);
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(imagePath);
            bitmap.compress(CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            Log.e(“GREC”, e.getMessage(), e);
        } catch (IOException e) {
            Log.e(“GREC”, e.getMessage(), e);
        }
    
        return imagePath.getAbsolutePath();
    } 
    

    分享到社交网络。

    【讨论】:

    • 似乎缺少最后一步。问题全在于如何共享图像(使用意图)虽然您说明了如何捕获图像,但缺少共享部分。
    猜你喜欢
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2015-12-17
    相关资源
    最近更新 更多