Delete photo thumbnail in Gallery after manually delete the photo file

我只是一个虾纸丫 提交于 2020-04-13 17:38:19

问题


case R.id.menu_delete:
    File photoToDelete = new File(photoPath, photoList[gPosition]);
    photoToDelete.delete();
    checkPhotoFolder();
    galleryAdapter.notifyDataSetChanged();
    Log.d("position", "" + gPosition);
    return true;

I'm manually delete a photo file using above code. But in the system gallery the photo still show the blank thumbnail. The question is how can I delete the photo file and also the thumbnail of it in the gallery?


回答1:


Try invoke the MediaScanner to refresh the gallery

// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
        new String[] { file.toString() }, null,
        new MediaScannerConnection.OnScanCompletedListener() {
    public void onScanCompleted(String path, Uri uri) {
        Log.i("ExternalStorage", "Scanned " + path + ":");
        Log.i("ExternalStorage", "-> uri=" + uri);
    }
});

Code from Google's example




回答2:


Try this.

getContentResolver().delete(Uri.fromFile(photoToDelete), null, null);



回答3:


You must update the data structure (eg. list or array) that stores the photos. I guess it is photoList. So you should do (assuming photoList is an array):

photoList = photoList.asList().remove(gPosition).toArray();


来源:https://stackoverflow.com/questions/13532285/delete-photo-thumbnail-in-gallery-after-manually-delete-the-photo-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!