当我们使用Intent打开图片浏览器如:
Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intent, 1);获取到的图片的地址如:
Uri originalUri = data.getData(); ContentResolver resolver = this.getContentResolver(); String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(originalUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); if( cursor.moveToFirst()) { String path = cursor.getString(column_index); }然而, 我们直接
File file =new File(path); if(file.exists()) file.delete();发现,执行后,再次打开图片浏览器,图片还是存在的。但是文件确实不存在了。然而是不是觉得很矛盾?
其实不茅盾,在系统数据库里,它保存的方式其实有2种,你删除的仅仅是物理文件而已。
图片浏览器显示的数据库记录里面的另外一种持久化,具体方式可能是byte方式(本人猜测),具体情况看文档
因此,我们只能通过内容提供者来间接清理这条数据库整条记录
解决方案:
ContentResolver resolver = this.getContentResolver(); //path 文件的路径 resolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA + "=?", new String[]{path});