开发者

saving image to internal memory and would appear in the gallery

开发者 https://www.devze.com 2023-04-03 08:56 出处:网络
I have search much of questions related to my question..Is there anyway to store an image in internal me开发者_Python百科mory and it should appear in the gallery as well? It\'s totally possible, I\'ve

I have search much of questions related to my question..Is there anyway to store an image in internal me开发者_Python百科mory and it should appear in the gallery as well?


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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消