Image disappears in Android Gallery when renamed

2019-06-08 23:58发布

I am trying to rename an image in Gallery taken from Camera to give a new name. When I renamed the file the original image disappears showing 2 black squares . Can any one help me in sorting this issue by telling me procedure to rename Gallery Image.

Path is as follows : /mnt/sdcard/DCIM/Camera

I tried to update the Image title through content provider as below:

ContentValues val = new ContentValues();
val.put(Images.Media.TITLE, "ImageTitle23");
getContentResolver().update(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, val, null, null);

The image title is changed in Gallery.

But when I try to query the updated image as below I am getting the name same as previous one :

final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
final String imageOrderBy = MediaStore.Images.Media._ID+" DESC";   
Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, MediaStore.Images.Media._ID+"="+id, null, imageOrderBy);  
if(imageCursor.moveToFirst()){    
  int idd = imageCursor.getInt(imageCursor.getColumnIndex(MediaStore.Images.Media._ID)); 
  String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA)); 

Can anyone help me in sorting out this issue?

2条回答
手持菜刀,她持情操
2楼-- · 2019-06-09 00:38

The MediaStore looks for changes in your pictures BUT it will not react to all changes in real time. Each time you do such a modification, you should ask the MediaScanner to scan this file:

MediaScannerConnection.scanFile(
  getApplicationContext(), 
  new String[]{file.getAbsolutePath()}, 
  null, 
  new OnScanCompletedListener() {
     @Override
     public void onScanCompleted(String path, Uri uri) {
        Log.v("grokkingandroid", 
              "file " + path + " was scanned seccessfully: " + uri);
     }
  });

Also, you should check this blog post that gives a lot of useful informations on this usecase.

查看更多
Summer. ? 凉城
3楼-- · 2019-06-09 00:56

The Gallery is interacting with the MediaStore content provider. If you change the name of the image file you probably need to update the field in the content provider.

You can read up on it in the MediaStore reference : http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html

Potentially you can just trigger a refresh of the MediaStore. Look at this thread : How can I refresh MediaStore on Android?

查看更多
登录 后发表回答