Reputation: 1420
this is the uri of image that i'm trying to share
/storage/emulated/0/CR7/ImageApr 21, 2018 6:40:00 PM.jpeg
on click redirected to package whatever I select whatsapp,telegram but getting error "sharing failed please try again"
rl_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkPermissions()) {
Uri uri5 = Uri.parse(myuri);
Intent t = new Intent(Intent.ACTION_SEND);
t.setType("image/*");
t.putExtra(Intent.EXTRA_STREAM, uri5);
startActivity(Intent.createChooser(t, "Share Image"));
}
}
});
Upvotes: 1
Views: 4500
Reputation: 558
You can't get uri by using Uri uri5 = Uri.parse(myuri);
on android 8.0 and above , you have to use file provider in your application to get uri for a file, also you need to add Intent.FLAG_GRANT_READ_URI_PERMISSION
to your share intent, then only the third application can get access to the file
for sample 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>
create dummy class which extends FileProcider in our case GenericFileProvider.java
import android.support.v4.content.FileProvider;
public class GenericFileProvider extends FileProvider {
}
in your manifest.xml add these lines inside application tag
<provider
android:name=".GenericFileProvider"
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>
now you can get Uri for File using the bellow code in your application
Uri screenshotUri = FileProvider.getUriForFile(SelectedImageActivity1.this, getApplicationContext().getPackageName() + ".provider", file);
Upvotes: 4