4ucai
4ucai

Reputation: 91

saving image to internal memory and would appear in the gallery

I have search much of questions related to my question..Is there anyway to store an image in internal memory and it should appear in the gallery as well?

Upvotes: 3

Views: 4319

Answers (2)

dumbfingers
dumbfingers

Reputation: 7089

It's totally possible, I've done this before.

Before you are saving an image to the internal memory(or called app folder, etc.), you'd better set Context.MODE_WORLDREADABLE. Although I set MODE_PRIVATE and can be read from gallery after I ran MediaScanner in my case, I recommend you set MODE_PRIVATE for the safety concern.

After you've saved your image to the internal memory, you can call MediaScanner like this:

// call media scanner to refresh gallery
MediaScannerConnection.scanFile(getApplicationContext(), new String[]{filePath}, null, new MediaScannerConnection.OnScanCompletedListener() {
    @Override
    public void onScanCompleted(String path, Uri uri) {
                    
        Log.i("MediaScanner", "Scanned " + path + ":");
        Log.i("MediaScanner", "-> uri=" + uri);
    }
});

Copy/Paste the code above should work. Then your image will appear to the gallery.

Reference of Context

Upvotes: 2

Billy Pham
Billy Pham

Reputation: 138

You can treat your internal memory as a directory and write your file on it. Sample code coulde be found here. Loading image from memory is the same with reading binary image file from directory. Then you convert the file you have read to Bitmap using decodeByteArray and display on your gallery.

This is another example of saving image and display on screen (but this one is on webview instead of gallery).

Upvotes: 0

Related Questions