【问题标题】:Sharing Failed in Intent分享意图失败
【发布时间】:2017-07-01 01:18:07
【问题描述】:

我正在尝试使用以下意图发送图像:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mArray[position]));
shareIntent.setType("image/png");
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(shareIntent);

编辑:文件图像数组

private void createDrawbleArray(File fileDir) {

    mArray = new File[Constants.TOTAL_GRID_ICONS];
    TypedArray tArray = getResources().obtainTypedArray(R.array._images);

    for (int i = 0; i < Constants.TOTAL_GRID_ICONS; i++) {
        mArray[i] = getFileForResource(this, tArray.getResourceId(i, -1), fileDir, "a" + i + ".png");
    }
    tArray.recycle();

}

编辑:权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

但是,不幸的是,它给我的分享失败了。 可能是什么问题?

谢谢。

【问题讨论】:

  • 你能分享你的图片文件位置吗?
  • 它的数组我创建为...编辑...
  • /data/user/0/com.example.android.supportv13.sampleime/files/images/a2.png
  • 你能说明你是如何从意图中检索 uri 的吗??
  • Manifest 中的权限列表

标签: android image android-intent


【解决方案1】:

终于找到了解决办法:

 Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType(getResources().getString(R.string.strIntentType));
    Uri uri = FileProvider.getUriForFile(mContext, getResources().getString(R.string.strFileProviderPackage), mArrayIcons[position]);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(shareIntent);

使用 FileProvider 这样做,它就像一个魅力..!!!耶。

【讨论】:

  • 嘿,还有一个小问题,与 WhatsApp 共享的图像使用黑色背景,因为我使用的是透明 png 图像。我希望它带有白色背景。有什么想法吗?
【解决方案2】:

由于您的图像文件位置在您的项目中,因此您无法与外部应用共享图像。

这里有几种分享方式

  1. 创建您的文件提供程序

  2. 将图像文件存储在外部目录中。

检查这些链接以创建您的文件提供程序

  1. https://developer.android.com/training/secure-file-sharing/setup-sharing.html

  2. https://developer.android.com/reference/android/support/v4/content/FileProvider.html

  3. http://www.blogc.at/2014/03/23/share-private-files-with-other-apps-fileprovider/

【讨论】:

  • 我正在使用 CommitContent API 的 InputMethodService。
  • 我可以成功分享“文本”。但是,问题在于图像。
  • 您好先生,感谢您的宝贵回答。 B
  • 您好先生,感谢您的宝贵回答。但是,我无法与 Facebook Messenger 分享图片。为什么 ?有什么想法吗?
  • 还有一件事,我正在创建虚拟键盘应用程序,我的键盘在另一个消息应用程序中打开。现在,如果该应用程序不支持提交内容 API,它将通过 Intent 共享。我正在检查特定应用程序是否支持提交内容 API。在 Facebook Messenger 的情况下条件成真。现在,我想在 Facebook Messenger 的情况下使用 ShareIntent。那么,我如何检查 Facebook Messenger 当前是否正在运行。注意:我正在使用 InputMethodService。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
  • 2014-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
相关资源
最近更新 更多