之前已经问过这个问题(不是特别喜欢这个)但是还没有一个All Exclusive答案。 所以我们试图find最好的解决方案。 我正在开发一个应用程序,在我的应用程序中,我通过将其文件移动到名为.myPic的目录来隐藏名为myPic的目录。 当我隐藏我的照片时,它的缩略图仍然在画廊中。 我find了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循环并传递了我隐藏的每个文件的旧地址。 这是我的MediaScannerConnectionfunction:
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怎么MediaScannerConnection ? 好吧,它将1.jpg从.myPic目录添加到图库,这正是我想要的。 所以再也不是一个All Exclusive方法
第三种方案:
使用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() ? 这可能是所有Exclusive方法,用于删除文件缩略图显示在库中的情况。 我有我的文件路径,我只需要从媒体商店内容提供商删除它。
更新:事实certificate在第三个解决方案中使用Uri.parse(path)是错误的。 image Uri以content://启动,可由MediaScannerConnection检索。 所以我创建了一个名为imageInGalleryUri的Uri并为其指定null值。 使用我的scanFile函数我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 uri=" + uri); imageInGalleryUri = uri; } }); }
我尝试了代码,但它只检测第一个图像并从图库中删除它,但不影响其他图像。 我无法弄清楚为什么。 任何想法?
提前谢谢你的帮助
。 文件夹之前只是让它不可见。 但是有办法说不要使用这个文件夹来画廊。 请尝试将名为“.nomedia”文件的空文件放入您的文件夹中。