Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Why my saved image is not appearing in the default gallery of my phone?
    primarykey
    data
    text
    <p>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:</p> <pre><code>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; } </code></pre> <p>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:</p> <pre><code>uriOfSucessfulySavedImage = c.insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, v); </code></pre> <p>The interesting thing about this, however, is that the variable <code>uriOfSucessfulySavedImage</code> is not null (it returns <code>content://media/internal/images/media/x</code>, 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 <code>MediaStore.Images.Media.EXTERNAL_CONTENT_URI</code>.</p> <p>Does anybody have any clue what is going on? How can I save an image into the internal storage of the phone <em>and have that image in the phone's gallery</em>?</p> <h2>Update</h2> <p>I forgot one important information. The File "f" in the parameters of the method <em>"saveMediaEntry"</em> is coming from this other method for when the SD card is mounted (that is, for the first code):</p> <pre><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; } </code></pre> <p>and from the following code for when the SD card <strong>is not</strong> founded:</p> <pre><code>public static File getCacheDirectory(String desiredNameOfTheDirectory, Context context){ File fileCacheDir = null; fileCacheDir = context.getCacheDir(); if(!fileCacheDir.exists()){ fileCacheDir.mkdirs(); } return fileCacheDir; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload