How to add a photo to MediaStore

I want to add the captured photos to MediaStore so that Gallery can find them (without restarting the device). App min sdk - 9. Any help, blog or documentation is appreciated.

+3
source share
4 answers

On most devices, all you have to do is wait a bit and new photos will be detected automatically.

If you want to pre-create an update in the gallery, you need to use the MediaScanner class, it will update the gallery - delete deleted photos, add new ones and so on ...

public void refreshGallery() {
    Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    String newPhotoPath = "file:" + image.getAbsolutePath(); // image is the created file image
    File file = new File(newPhotoPath);
    Uri contentUri = Uri.fromFile(file);
    scanIntent.setData(contentUri);
    sendBroadcast(scanIntent);
}

Hope this helps!

+5
source
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

Insert this line of code after the save code.

, ( ".nomedia" ) .

.

MediaScanner.

// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
        new String[] { file.toString() }, null,
        new MediaScannerConnection.OnScanCompletedListener() {
    public void onScanCompleted(String path, Uri uri) {
        Log.i("ExternalStorage", "Scanned " + path + ":");
        Log.i("ExternalStorage", "-> uri=" + uri);
    }
});

Google.

+1

ok , , , Android-,

 getallimages(Environment.getExternalStorageDirectory());

private void getallimages(File dir)
    {

                String[] STAR = { "*" };

        final String orderBy = MediaStore.Images.Media.DEFAULT_SORT_ORDER;
        Cursor imagecursor = cntx.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STAR, null, null, orderBy);
        int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
        int count = imagecursor.getCount();
        for (int i = 0; i < count; i++) {
            imagecursor.moveToPosition(i);
            int id = imagecursor.getInt(image_column_index);
            ImageItem imageItem = new ImageItem();

            if(new File(imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA))).length()<=10485760)
            {
                imageItem.filePath = imagecursor.getString(imagecursor.getColumnIndex(MediaStore.Images.Media.DATA));

                    imageItem.id = id;
                    imageItem.selection = false; //newly added item will be selected by default
                    controller.images.add(imageItem);   

            }
 }

}
0

You can ask MediaScanner to scan a specific file, that is, your image file on demand. This should result in less overhead than just asking MediaScanner to scan everything for new files.

SO: how to run a media scanner in android

0
source

All Articles