Android - Why my saved image is not appearing in the default gallery of my phone?
Asked Answered
T

5

5

I am trying to save an image from my application to the default gallery of my phone. The code below works perfectly if I have a SD card on the phone. The image saved appears in the phone's gallery and everything, as expected:

private Uri saveMediaEntry(File f, String title, String description, int orientation,      Location loc) {

    ContentValues v = new ContentValues();
    v.put(Images.Media.TITLE, title);
    v.put(Images.Media.DISPLAY_NAME, title);
    v.put(Images.Media.DESCRIPTION, description);

    v.put(Images.Media.ORIENTATION, orientation);

    String nameFile = f.getName();
    File parent = f.getParentFile() ;
    String path = parent.toString().toLowerCase() ;
    String nameParent = parent.getName().toLowerCase() ;
    v.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
    v.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, nameParent);
    v.put(Images.Media.SIZE,f.length()) ;

    if( nameFile.toLowerCase().contains(".png") ){
        v.put(Images.Media.MIME_TYPE, "image/png");

    }else if( nameFile.toLowerCase().contains(".jpg") || 
              nameFile.toLowerCase().contains(".jpeg") ){
         v.put(Images.Media.MIME_TYPE, "image/jpeg");

    }else{
        v.put(Images.Media.MIME_TYPE, "image/jpeg");
    }

    String imagePath = f.getAbsolutePath();
    v.put("_data", imagePath) ;
    ContentResolver c = getContentResolver() ;

    Uri uriOfSucessfulySavedImage = null;
    uriOfSucessfulySavedImage = c.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, v);

    return uriOfSucessfulySavedImage;
  }

However, if I try to save the same image into the internal storage (for when the phone does not have a SD card), the image does not appear in the phone's gallery! To try to do that, I only change one line from the above code:

uriOfSucessfulySavedImage = c.insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, v);

The interesting thing about this, however, is that the variable uriOfSucessfulySavedImage is not null (it returns content://media/internal/images/media/x, where 'x' is a number). So, the image is being saved somewhere in the internal storage of the phone, but it is not getting displayed in the phone gallery's as when I use MediaStore.Images.Media.EXTERNAL_CONTENT_URI.

Does anybody have any clue what is going on? How can I save an image into the internal storage of the phone and have that image in the phone's gallery?

Update

I forgot one important information. The File "f" in the parameters of the method "saveMediaEntry" is coming from this other method for when the SD card is mounted (that is, for the first code):

public static File getCacheDirectory(String desiredNameOfTheDirectory){

File fileCacheDir = null;

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ){

        fileCacheDir = new File( Environment.getExternalStorageDirectory(), desiredNameOfTheDirectory );
   }


    if(!fileCacheDir.exists()){
        fileCacheDir.mkdirs();
    }

    return fileCacheDir;
}

and from the following code for when the SD card is not founded:

public static File getCacheDirectory(String desiredNameOfTheDirectory, Context   context){

    File fileCacheDir = null;

        fileCacheDir = context.getCacheDir();

    if(!fileCacheDir.exists()){
        fileCacheDir.mkdirs();
    }

    return fileCacheDir;
}
Theurich answered 18/7, 2012 at 23:46 Comment(1)
possible duplicate of Image, saved to sdcard, doesn't appear in Android's Gallery appPhysicochemical
O
6

I haven't tried this, but I believe you need to run the Media Scanner to scan the internal storage directory so that the gallery can see your newly saved image. Check this post here.

Oliana answered 19/7, 2012 at 2:49 Comment(3)
Thank you Henry. I will try that out.Theurich
I have just restarted my phone. All the images that I had saved in the internal storage before are now being shown in the gallery! So, it definitely has something to do with this Media Scanner you posted. Thanks! I will write some code later on to scan the media and post the solution.Theurich
Awesome! It worked! I have just used the SingleMediaScanner class of that post you referenced. Thanks a lot Henry!Theurich
O
8

Another easy way to do it. Add this after saving your file.

File imageFile = ...
MediaScannerConnection.scanFile(this, new String[] { imageFile.getPath() }, new String[] { "image/jpeg" }, null);
Olette answered 9/4, 2014 at 2:11 Comment(0)
O
6

I haven't tried this, but I believe you need to run the Media Scanner to scan the internal storage directory so that the gallery can see your newly saved image. Check this post here.

Oliana answered 19/7, 2012 at 2:49 Comment(3)
Thank you Henry. I will try that out.Theurich
I have just restarted my phone. All the images that I had saved in the internal storage before are now being shown in the gallery! So, it definitely has something to do with this Media Scanner you posted. Thanks! I will write some code later on to scan the media and post the solution.Theurich
Awesome! It worked! I have just used the SingleMediaScanner class of that post you referenced. Thanks a lot Henry!Theurich
P
3

Copy Past this Function in your Activity

private void scanner(String path) {

        MediaScannerConnection.scanFile(FrameActivity.this,
                new String[] { path }, null,
                new MediaScannerConnection.OnScanCompletedListener() {

                    public void onScanCompleted(String path, Uri uri) {
                        Log.i("TAG", "Finished scanning " + path);
                    }
                });
    }

And then add this Line where you save your image

scanner(imageFile.getAbsolutePath());
Piccadilly answered 31/1, 2018 at 13:32 Comment(1)
Worked like champPalingenesis
N
0

Try this.

Write down this line once image stored in gallery.

File file = ..... // Save file

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
Notions answered 27/3, 2015 at 8:19 Comment(0)
S
0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

                sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                        Uri.parse("file://"
                                + Environment.getExternalStorageDirectory())));
            } else {
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                        Uri.parse("file://"
                                + Environment.getExternalStorageDirectory())));
            }
Selfsealing answered 9/8, 2015 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.