在意图中共享失败

时间:2017-02-13 06:01:39

标签: android image android-intent

我正在尝试使用intent发送图像,如下所示:

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" />

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

感谢。

2 个答案:

答案 0 :(得分:2)

最后,我得到了解决方案:

 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这样做,它就像一个魅力.. !!! yeppii。

答案 1 :(得分:1)

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

以下是分享

的几种方法
  1. 创建文件提供程序

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

  3. 检查这些链接以创建文件提供程序

    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/

相关问题