kekko986
kekko986

Reputation: 81

how update Android MediaStore after i move a JPG file

my app move .jpg file to others folders and to get viewable in the stock gallery i have sendBroadcast ACTION_MEDIA_MOUNTED

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + 
            Environment.getExternalStorageDirectory() ))); 

but this take much time.. i have understand (maybe) that i have to update manually with cursor/contentResolver in mediaStore directly to get this faster. can anyone help me on this? thanks.. my code actually is:

 Uri uri = (Uri) list.get(cont); 
 Cursor cursor = managedQuery(uri, proj, null, null, null);
 int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
 cursor.moveToFirst();
 String app = cursor.getString(column_index);
 File orig =  new File( app.toString());
 File dest = new File( destination_path +"/"+ orig.getName().toString());
 orig.renameTo(dest);

with this i move a file from a path to another one.

after this, to get images in gallery i have to sendBroadcast ACTION_MEDIA_MOUNTED

Upvotes: 5

Views: 4564

Answers (2)

prom85
prom85

Reputation: 17848

I just had to do the same, use following to update the MediaStore:

ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, newPath);
boolean successMediaStore = context.getContentResolver().update(
    MediaStore.<TYPE>.Media.EXTERNAL_CONTENT_URI, values,
    MediaStore.MediaColumns.DATA + "='" + oldPath + "'", null) == 1;

Replace <TYPE> with the correct media store... (Images, Video, Audio...)

Upvotes: 4

Krylez
Krylez

Reputation: 17810

Use the MediaScannerConnection to update the OS:

MediaScannerConnection.scanFile(this,
          new String[] { file.toString() }, null,
          new MediaScannerConnection.OnScanCompletedListener() {
      public void onScanCompleted(String path, Uri uri) {
          // code to execute when scanning is complete
      }
 });

Upvotes: 0

Related Questions