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).
精彩评论