Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
}
like image 225
Bitcoin Cash - ADA enthusiast Avatar asked Dec 15 '22 21:12

Bitcoin Cash - ADA enthusiast


1 Answers

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);
like image 102
Lazy Ninja Avatar answered Dec 28 '22 07:12

Lazy Ninja