Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make image File visible in Android Gallery on Lollipop

I am trying to make some pictures that are taken in the app visible in the Gallery (so they can be shared and looked at outside of the app), but I want to keep the images themselves inside the data directory of the app so when the app is deleted they get removed (so they are stored in "{sdcard}/Android/data/{appID}/Pictures/{subfolder}/").

I have looked at a lot of answers and for older versions of Android both of the following solutions work, but they don't seem to work in Lollipop:

MediaScannerConnection.scanFile(this,
    new String[]{file.toString()}, new String[] { "image/jpeg" },
    new MediaScannerConnection.OnScanCompletedListener() {
        public void onScanCompleted(String path, Uri uri) {
            Log.i("ExternalStorage", "Scanned " + path + ":");
            Log.i("ExternalStorage", "-> uri=" + uri);
        }
    });

Uri contentUri = Uri.fromFile(file);
Intent mediaScanIntent = new 
    Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,contentUri);
sendBroadcast(mediaScanIntent);

I have tested this in a Nexus 4 (Android 5.0.1) and a Nexus 6 (Android 5.1) and it doesn't work. In a Nexus 7 (Android 4.2.2) it does work. All 3 devices have a ".onmedia" file in the {sdcard}/Android/data/ folder, so no normal media scan should pick them up...

Does anyone know a work around to get this to work on all devices/Android versions? Should this because of the noMedia file not work in the first place and is Lollipop the only version that does this correctly?

Edit: Note that even a restart of the phone (which should also trigger a rescan of files) doesn't work. And for good reason as the files are in a subfolder of a ".nomedia" directory... So any solution containing a MEDIA_MOUNTED broadcast is kind of useless I assume.

like image 328
Kasium Avatar asked Apr 23 '15 12:04

Kasium


People also ask

Why is my picture not showing in my gallery?

Enable Media Visibility Setting If you are using an Android phone, your media visibility setting may be one reason why WhatsApp images and videos are not showing in Gallery. Once you download a photo or video from WhatsApp, it should be visible in your Gallery.

Why some photos are not showing in gallery Android?

If your photos are visible in My Files but are not in the Gallery app, these files may be set as hidden. This prevents Gallery and other apps from scanning for media. To solve this, you can change the option for showing hidden files.

How do I get my photos to show in my gallery?

Go to Settings -> Apps / Application manager -> search for Gallery -> open Gallery and tap on Clear Data . Switch off your phone and wait for few minutes (say 2-3 min) and then switch on and wait for few minutes.


2 Answers

Try below code. It working for me in Lollipop, Hope working for you as well

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA,"file path");
values.put(MediaStore.Images.Media.MIME_TYPE,"image/jpeg");
mContext.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);

EDIT: Don't forget to add this to your manifest (although in Lollipop it should no longer be necessary):

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 84
Lokesh Avatar answered Oct 14 '22 09:10

Lokesh


I would suggest you to take a look at my answer here. It wraps up all the findings around MediaScanner that I've made.

Side note: all the KitKat issues seem to be sticking on Lollipop as well.

like image 35
Sebastiano Avatar answered Oct 14 '22 09:10

Sebastiano