Android - Why my saved image is not appearing in t

2019-06-27 10:46发布

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;
}

5条回答
狗以群分
2楼-- · 2019-06-27 11:29

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);
查看更多
Ridiculous、
3楼-- · 2019-06-27 11:34

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.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-06-27 11:34

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)));
查看更多
该账号已被封号
5楼-- · 2019-06-27 11:49
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())));
            }
查看更多
一夜七次
6楼-- · 2019-06-27 11:49

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());
查看更多
登录 后发表回答