Share image with other apps

2019-04-28 16:32发布

问题:

Here is my problem... I want to share a png image. I have the image in drawable and assets. When I use the sharingIntent it works but not in the way that I want. The file shared appears with numbers and without extension and some apps send the message "unknow file". What can i do??

this is my code:

    Uri uri = Uri.parse("android.resource://com.example.shareimages/" + R.drawable.caja);
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);

    shareIntent.setType("image/png");

    shareIntent.putExtra(Intent.EXTRA_STREAM,  uri);

    startActivity(Intent.createChooser(shareIntent, "send"));

I tried to use instead of drawable files, the files in assets ( with this "file:///android_asset/...) but it didn't work

回答1:

Try something like this:

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("image/*");

// For a file in shared storage.  For data in private storage, use a ContentProvider.
Uri uri = Uri.fromFile(getFileStreamPath(pathToImage));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(shareIntent)

If you want to share it via the assets folder, you'll have to use a ContentProvider. See this answer on how to do so:

https://stackoverflow.com/a/7177103/1369222