0

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?

laalto
  • 150,114
  • 66
  • 286
  • 303
user1276092
  • 523
  • 3
  • 8
  • 30

2 Answers2

2

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.

Teovald
  • 4,369
  • 4
  • 26
  • 45
0

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?

Community
  • 1
  • 1
Luigi Agosti
  • 925
  • 10
  • 19
  • 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);
    – user1276092 Sep 21 '12 at 18:25
  • Modified code as said by you, but still getting the same name when I tried to query after updating the image. – user1276092 Sep 21 '12 at 18:30