当前位置: 代码迷 >> Android >> 意向共享失败
  详细解决方案

意向共享失败

热度:53   发布时间:2023-08-04 10:46:59.0

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

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

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

谢谢。

终于,我得到了解决方案:

 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来做到这一点,它就像一个魅力.. !!! 是的。

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

这是分享的几种方法

  1. 创建您的文件提供者

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

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

  相关解决方案