android share image in ImageView without saving in sd card
Asked Answered
L

3

15

I want to Share the image in image view.but i don't want save to SDcard. But when i use Intent to share i used code

Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("image/jpeg");
                share.putExtra(Intent.EXTRA_STREAM,Uri.parse(path));
                startActivity(Intent.createChooser(share, "Share Image"));  

here Path specified location of image in sdcard

but i don't want save image ... is there possible ..

Lessard answered 20/12, 2012 at 10:30 Comment(0)
U
2

You could also share it as the Media Gallery content provider URI (if the image is already on the phone, or if it came from the web you could share the URL (although it's not the same effect).

But if came from the web and you directly decoded to Bitmap and now want to share it as a proper image, yeah, you really need the file!

Unstained answered 20/12, 2012 at 10:36 Comment(2)
I cant believe there is no way to do this with out saving the image to a file!Sailfish
without saving to sdcard It's Possible,Andie
B
30

The recommended method for sharing files with other apps is with a ContentProvider called FileProvider. The documentation is pretty good, but some parts are a little tricky. Here is a summary.

Set up the FileProvider in the Manifest

<manifest>
    ...
    <application>
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.myapp.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
        ...
    </application>
</manifest>

Replace com.example.myapp with your app package name.

Create res/xml/filepaths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="shared_images" path="images/"/>
</paths>

This tells the FileProvider where to get the files to share (using the cache directory in this case).

Save the image to internal storage

// save bitmap to cache directory
try {

    File cachePath = new File(context.getCacheDir(), "images");
    cachePath.mkdirs(); // don't forget to make the directory
    FileOutputStream stream = new FileOutputStream(new File(cachePath, "image.png")); // overwrites this image every time
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    stream.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Share the image

File imagePath = new File(context.getCacheDir(), "images");
File newFile = new File(imagePath, "image.png");
Uri contentUri = FileProvider.getUriForFile(context, "com.example.app.fileprovider", newFile);

if (contentUri != null) {

    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
    shareIntent.setDataAndType(contentUri, getContentResolver().getType(contentUri));
    shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
    startActivity(Intent.createChooser(shareIntent, "Choose an app"));

}

Further reading

Berkman answered 11/5, 2015 at 15:54 Comment(3)
For AndroidX - Use androidx.core.content.FileProviderHujsak
For some reason using only setDataAndType or putExtra(Intent.EXTRA_STREAM, ...) was not working for me. Using both solved itDemb
Why I am getting black/white bg on image share? how can I avoid this?Sublet
R
5

I was able to do this:

File file = new File( getCacheDir(), "screenshot.png");
...
file.setReadable(true, false);
Uri uri = Uri.fromFile(file);
...
intent.putExtra(Intent.EXTRA_STREAM, uri);

This way I save the file in my own cache folder, so I'm not fooling with any public folders or depending on the sd card being present.

Also, this way it gets deleted automatically if the user removes my app, but I also used startActivityForResult/onActivityResult to delete the file and set its folder back to private when the share is finished.

(Personally I was hoping to find a way to share a bitstream and avoid the step of creating a file entirely, but I don't think that is possible.)

[EDIT: I discovered this doesn't work on 2.3.6 where the file MUST be on file:///mnt/sdcard.]

Remanence answered 6/6, 2013 at 22:22 Comment(3)
Are you able to share the image with gmail ? I got some error message when I tried.Progressionist
As this comment says about your answer, "Creating world-readable files on your app's internal storage is not a good idea." Use FileProvider to do it more securely.Berkman
Hi Tom, your answer is great! Do you know what is the minimum required API version for this to work, i.e. shall it work under Android 3.0+? Thank you.Dawes
U
2

You could also share it as the Media Gallery content provider URI (if the image is already on the phone, or if it came from the web you could share the URL (although it's not the same effect).

But if came from the web and you directly decoded to Bitmap and now want to share it as a proper image, yeah, you really need the file!

Unstained answered 20/12, 2012 at 10:36 Comment(2)
I cant believe there is no way to do this with out saving the image to a file!Sailfish
without saving to sdcard It's Possible,Andie

© 2022 - 2024 — McMap. All rights reserved.