这个问题之前已经被问过了(不是像这样具体的问题),但没有一个全面的答案。所以我们在这里尝试找到最佳解决方案。我正在开发一个应用程序,在我的应用程序中,我通过将文件移动到名为.myPic
的目录中来隐藏名为myPic
的目录。当我隐藏我的图片时,它们的缩略图仍然在相册中。我找到了三种解决方法:
第一种解决方案:
使用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
只会对已存在的文件产生影响。例如,假设我有一张名为1.jpg
的图片在myPic
目录中,使用这个类可以立即将1.jpg
添加到我的相册中,但是当我将1.jpg
移动到.myPic
目录并扫描旧路径1.jpg
时,什么都没有发生。Logcat显示该文件不存在。因此MediaScannerConnection
只能将文件添加到相册中。如果我将1.jpg
的新路径传递给MediaScannerConnection
会怎样呢?它会将.myPic
目录中的1.jpg
添加到相册中,这恰好不是我想要的。因此,这不是一个全包含的方法。
第三种解决方案:
使用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)
是错误的。 图像 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;
}
});
}
我尝试了这段代码,但它只检测到第一张图片并从相册中删除它,但不影响其他图像。我想不出为什么。有任何想法吗?
提前感谢您的帮助。
.nomedia
,并且不适用于每个Android版本。我们正在寻找一个全面的答案。 - hadi