本文介绍了Intent.ACTION_SEND无法在Oreo上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个自定义相机应用程序,它捕获图片并将其存储在图库中。当我使用Intent.ACTION_SEND共享该图像时,除了具有 API 26的设备(即OREO)之外,它在所有设备上都能正常工作。

I am developing a custom camera application which captures a picture and stores it in the gallery. When I share that image using Intent.ACTION_SEND, it works absolutely fine on all devices except for devices having API 26, i.e. OREO.

My共享图像的代码是:

My code to share the image is:

Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.setType("image/jpeg");
            Uri uriShare = Uri.fromFile(outFile);
            //outfile is the path of the image stored in the gallery
            shareIntent.putExtra(Intent.EXTRA_STREAM, uriShare);
            startActivity(Intent.createChooser(shareIntent, ""));

任何人都可以帮我解决这个问题吗?

Can anyone help me resolve this issue?

推荐答案

如果 targetSdkVersion 高于 24 ,那么用于授予访问权限。

If targetSdkVersion is higher than 24, then FileProvider is used to grant access.

创建一个xml文件(路径:res \ xml) provider_paths.xml

Create an xml file(Path: res\xml) provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

Add a Provider in AndroidManifest.xml

 <provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

替换

Uri uri = Uri.fromFile(fileImagePath);

Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",fileImagePath);

你完成了谢谢。

这篇关于Intent.ACTION_SEND无法在Oreo上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 10:05
查看更多