I am trying to share an image through implicit intent. The image is shared successfully through Messenger and Twitter but fails on Facebook News Feed, Viber and Email. I read somewhere that I need to save the image to the external or internal SD card first before sharing. I am a little lost on 1) how to save the image to the SD card and 2) how to point to the new image location on the SD card in order to share it. Currently my code for sharing the image looks like this:
Button share = findViewById(R.id.sharetoapps);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("android.resource://com.testing.mypic/drawable/bad_day");
intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM,uri);
intent.setType("image/png");
startActivity(Intent.createChooser(intent, "share to:"));
}
});
And my xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/sharetoapps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="share" />
</LinearLayout>
If you have any pointers or know of any relevant tutorials I would appreciate it.
OK I finally got it to work but it requires a number of things.
First the following needs to be added to the AndroidManifest.xml file:
Then I had to create a folder named xml under res. Then I created a file called file_paths.xml inside that folder. The file contains the following:
Finally my share method ended up looking like this:
Note:
When I try to share the image in more than one place I get a warning saying my application keeps stopping and asking me whether I want to report the app (which I don't at present) though it still allows me to continue sharing the image. I am not sure what is causing this. If anyone has an idea please let me know.