通过意图共享图像失败

时间:2020-10-18 13:59:07

标签: android image android-intent

我正在尝试使用意图共享图像。请找到相同的功能。

        if(view.getId()==R.id.sendImage){
        Uri imgUri=Uri.parse("android.resource://com.example.watchyalist/mipmap/"+R.mipmap.test);
        intent=new Intent(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM,imgUri);
        intent.putExtra(intent.EXTRA_TEXT,"Please find the image");
        chooser=Intent.createChooser(intent,"Choose the app");
        startActivity(chooser);
    }

这里测试是我指的图像,当通过whatsapp共享时,它将发送bin文件。我尝试使用ic_launcher,但提示共享失败。这是清单文件的快照

 android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

我无法理解。我尝试将manifest标签更改为drawable,并因此将imag保留在drawable中,并进行相应的更改仍然没有运气。我将图像保存在项目的mipmap-anydpi-v26文件夹中,还尝试更改位置但没有用。有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:1)

EXTRA_STREAMdocumented,采用Uri方案的content:android.resource:不是content:。无需任何应用从他们的应用收到您的Uri时就知道如何处理它们。另外,您的Uri以半随机数结尾(无论R.mipmap.test恰好适用于此版本)。

请将该图像复制到文件中(例如,在getCacheDir()中),然后使用FileProvidergetUriForFile()获得Uri用于EXTRA_STREAM