【发布时间】:2017-08-29 18:31:32
【问题描述】:
这个问题之前已经被问过(不是特别像这样),但还没有一个 All Exclusive 的答案。所以我们试图在这里找到最好的解决方案。我正在开发一个应用程序,在我的应用程序中我隐藏了一个名为myPic 的目录,方法是将它的文件移动到一个名为.myPic 的目录中。当我隐藏我的照片时,它的缩略图仍在画廊中。我找到了 3 个解决方案:
第一个解决方案:
像这样使用 ACTION_MEDIA_MOUNTED 广播:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
此代码的问题在于它占用大量资源,最重要的是它自 android 4.4 起被阻止。所以使用这种方法在图库中添加 10 张图片是不合理的。所以它不是一个全排他的方法。同样使用ACTION_MEDIA_SCANNER_SCAN_FILE 也不适用于android 4.4
第二种解决方案:
使用MediaScannerConnection。所以我创建了一个for 循环并传递我隐藏的每个文件的旧地址。这是我的MediaScannerConnection 函数:
private void scanFile(File file) {
// 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);
}
});
}
关于MediaScannerConnection 的事情是它仅在文件存在时才有效。所以假设我在myPic 目录中有一张名为1.jpg 的图片。使用这个类,我可以立即将1.jpg 添加到我的画廊,但是当我将1.jpg 移动到.myPic 目录并且我扫描1.jpg 的旧路径时,什么也没有发生。 logcat 说这个文件不存在。所以MediaScannerConnection 只将文件添加到图库。如果我将1.jpg 的新路径传递给MediaScannerConnection 会怎样?好吧,它将1.jpg 从.myPic 目录添加到画廊,这正是我想要的不是。所以再次不是完全排他的方法
第三种解决方案:
使用getContentResolver()。所以对于删除缩略图,这种方法可能是最终的解决方案。所以我写了打击代码。在每个循环中,我都会检索图像的路径并将其传递给getContentResolver().delete(Uri.parse(path),null,null)。这是代码:
File myPic = new File(Environment.getExternalStorageDirectory()+"/myPic");
File myPicHide = new File(Environment.getExternalStorageDirectory()+"/.myPic");
if (!(myPicHide.exists()) & !(myPicHide.isDirectory())) {
myPicHide.mkdirs();
};
if (myPic.isDirectory()) {
String[] childeren = myPic.list();
if (childeren.length > 0) {
for (int i = 0; i < childeren.length; i++) {
String fileName = childeren[i];
File from = new File(Environment.getExternalStorageDirectory()+"/myPic"+fileName);
File to = new File(Environment.getExternalStorageDirectory()+"/.myPic"+fileName);
from.renameTo(to);
try {
String path = from.toString();
getContentResolver().delete(Uri.parse(path),null,null);
} catch(Exception e) {
Log.d("Rename", "Error happened");
}
}
}
} else {
Toast.makeText(getApplicationContext(), "myPic directory not found", Toast.LENGTH_LONG).show();
}
但它也不起作用,我的文件的缩略图仍然显示在厨房中。那么我是否以错误的方式使用getContentResolver()?对于已删除文件缩略图出现在图库中的情况,这可能是所有独占方法。我有我的文件路径,我只需要从媒体存储内容提供商中删除它。
更新:
事实证明,在第三个解决方案中使用Uri.parse(path) 是错误的。 image Uri 以content:// 开头,可以通过MediaScannerConnection 检索。所以我创建了一个名为imageInGalleryUri 的Uri 并为其分配null 值。使用我的scanFile 函数,我不时更改它的值并将它的值传递给getContentResolver()。这是代码:
boolean whereIsMediaState = true;
Uri imageInGalleryUri = null;
File myPic = new File(Environment.getExternalStorageDirectory()+"/myPic");
File myPicHide = new File(Environment.getExternalStorageDirectory()+"/.myPic");
if (!(myPicHide.exists()) & !(myPicHide.isDirectory())) {
myPicHide.mkdirs();
};
if (myPic.isDirectory()) {
String[] childeren = myPic.list();
if (childeren.length > 0) {
for (int i = 0; i < childeren.length; i++) {
String fileName = childeren[i];
File from = new File(Environment.getExternalStorageDirectory()+"/myPic"+fileName);
scanFile(from);
File to = new File(Environment.getExternalStorageDirectory()+"/.myPic"+fileName);
from.renameTo(to);
if (to.isFile()){
try {
getContentResolver().delete(imageInGalleryUri,null,null);}
catch(Exception e) {
Log.d("Rename", "Error happened");
}
}
}
} else {
Toast.makeText(getApplicationContext(), "myPic directory not found", Toast.LENGTH_LONG).show();
}
private void scanFile(File file) {
// 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);
imageInGalleryUri = uri;
}
});
}
我尝试了代码,但它只检测到第一张图像并将其从图库中删除,但不会影响其他图像。我不知道为什么。有什么想法吗?
提前感谢您的帮助
【问题讨论】: