First of all, be sure to add permissions to the manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
Create a file in the res> xml folder as "provider_paths.xml"
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="path_name" path="." /> </paths>
Create the provider tag in the manifest:
<provider android:name=".utils.FileAccessProvider" android:authorities="${applicationId}.path_name.file.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider>
This will allow access to file URIs for publication.
Intent intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android"); if (intent != null) { Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setPackage("com.instagram.android"); File logFile = new File(path); if (logFile.exists()) { Uri logFileUri = FileAccessProvider.getUriForFile(context, getApplicationContext().getPackageName() + ".path_name.file.provider", logFile); // shareIntent.setDataAndType(logFileUri, "image/*"); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); shareIntent.putExtra(Intent.EXTRA_STREAM, logFileUri); } shareIntent.setType("image/*"); startActivity(shareIntent); } else { // bring user to the market to download the app. // or let them choose an app? intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setData(Uri.parse("market://details?id=" + "com.instagram.android")); startActivity(intent); }
Remember to add Intent.FLAG_GRANT_READ_URI_PERMISSION
Without the above permissions, the error "unable to share."
source share