简体   繁体   English

在无法在Oreo 8.0中运行的其他应用程序上共享图像

[英]Share image on other apps not working in Oreo 8.0

I am using this code to share the image on Whatsapp, Facebook, and Instagram etc. This code works fine below API 25 but Not Working above API 25. 我正在使用此代码在Whatsapp,Facebook和Instagram等上共享图像。此代码在API 25以下可以正常工作,但在API 25以上不能正常工作。

Intent share = new Intent("android.intent.action.SEND");
                share.setType("image/jpeg");
                share.putExtra("android.intent.extra.STREAM", Uri.parse(this.imgUrl));
                startActivity(Intent.createChooser(share, "via"));

First make sure you have added permission 首先确保您已添加权限

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

Then use this code 然后使用此代码

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
Uri imageUri =  Uri.parse(this.imgUrl);                                
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Select"));

If targetSdkVersion is higher than 24, then Provider is used to grant access. 如果targetSdkVersion高于24,则使用Provider授予访问权限。

Create an xml file : res\\xml\\provider_paths.xml 创建一个xml文件: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>

now Add a Provider in AndroidManifest.xml file 现在在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>

and replace this with your code 并将其替换为您的代码

Intent share = new Intent("android.intent.action.SEND");
            share.setType("image/jpeg");
Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID+ ".provider",new File(this.imgUrl));
            share.putExtra("android.intent.extra.STREAM", uri);

            startActivity(Intent.createChooser(share, "via"));

Update 更新

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

Try this: 尝试这个:

Intent shareIntent = new Intent();
     shareIntent.setAction(Intent.ACTION_SEND); 
     shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage); 
     shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent,getResources().getText(R.string.send_to)));

Use File Provider for getting IMAGE URI and add flags to the intent... 使用文件提供程序获取IMAGE URI并向目标添加标志...

follow these steps carefully.. android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData() This update has been done since nougat.. 仔细按照以下步骤操作。android.os.FileUriExposedException:file:///storage/emulated/0/test.txt通过Intent.getData()在应用程序之外公开 。自牛轧糖以来,此更新已完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM