Reputation: 1064
I am using a sharing functionality to social application using Intent
.
I have a problem with sharing an image in Instagram.
Some times I get the message
Unable to load Image.
Here is my code:
String path="content://media/external/images/media/32872";
Intent shareIntent = new Intent();
shareIntent.setType("image/jpeg");
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));
shareIntent.setPackage("com.instagram.android");
startActivity(shareIntent);
How do I get rid of this problem?
Upvotes: 12
Views: 5419
Reputation: 11
First of all make sure to add permissions in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
Create a file in 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 a provider tag in 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 to be posted.
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);
}
Don't forget to add
Intent.FLAG_GRANT_READ_URI_PERMISSION
Without the above permissions, there was this "unable to share" error.
Upvotes: 0
Reputation: 1
Try this:
File filepath = Environment.getExternalStorageDirectory();
cacheDir = new File(filepath.getAbsolutePath() + "/LikeIT/");
cacheDir.mkdirs();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filepath.getAbsolutePath()));
activity.startActivity(intent);
Upvotes: 0