0

I'm using org.apache.commons.io.FileUtils to move files from one directory to another, like so:

File source = new File("/my/image/directory/image.jpg";
File destination = new File("/new/image/directory/image.jpg");
FileUtils.moveFile(source, destination);

Note that the source directory is an external directory and destination is an internal directory.

After above code is executed, I query to get the files of my internal directory like so:

File vaultDir = ctx.getDir("dir_name", Context.MODE_PRIVATE);
String[] fileList = vaultDir.list();
//Iterate and print file names

This is working as intended. But when i query the MediaStore like so:

ArrayList<GridViewObject> objects = new ArrayList<>();

String[] mProjection = {MediaStore.Images.Media.DISPLAY_NAME,
        MediaStore.Images.Media.DATA};

ContentResolver cr = ctx.getContentResolver();
Cursor mCursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        mProjection,
        null,
        null,
        MediaStore.Images.Media.DEFAULT_SORT_ORDER);

mCursor.moveToFirst();
while (!mCursor.isAfterLast()) {
    GridViewObject tmpGridViewObject = new GridViewObject();
    tmpGridViewObject.setTitle(mCursor.getString(0));
    tmpGridViewObject.setUrl(mCursor.getString(1));
    objects.add(tmpGridViewObject);
    mCursor.moveToNext();
}

The image is still returned to me by the query. Also, when I open my camera gallery app, the image is still there, as if it was never deleted.

I noticed that if I restart the phone, the image disappears from my device camera gallery app as intended (and above query return correct result). So I think it might be an indexing problem.

What do I need to do to "update" the MediaStore to show the intended result?

Marcus
  • 6,697
  • 11
  • 46
  • 89

1 Answers1

1

I solved this by instead of deleting the file using FileUtils.delete(srcFile, destFile); i copied the the file using FileUtils.copy(srcFile, destFile); and then to refresh MediaStore i used the following code to delete the source entry:

String selection = MediaStore.Images.Media.DATA + "='" + source.getAbsolutePath() + "'";
ctx.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, null);

Credit goes to Pascal's answer to this question.

Marcus
  • 6,697
  • 11
  • 46
  • 89