隐藏时,从库中删除图像缩略图

had*_*adi 24 java android

之前已经问过这个问题(不是特别喜欢这个)但是还没有一个All Exclusive答案.所以我们试图找到最好的解决方案.我正在开发一个应用程序,在我的应用程序中,我隐藏了一个名为myPic将其文件移动到名为的目录的目录.myPic.当我隐藏我的照片时,它的缩略图仍然在画廊中.我找到了3个解决方案:

第一个解决方

使用ACTION_MEDIA_MOUNTED广泛投射如下:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
Run Code Online (Sandbox Code Playgroud)

这段代码的问题在于它需要拥抱资源,最重要的是它因为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);
            }
        });
}
Run Code Online (Sandbox Code Playgroud)

关键MediaScannerConnection是它只在文件存在时才会生效.所以我说我有一张1.jpgmyPic目录中调用的图片.使用这个类我可以1.jpg立即添加到我的画廊,但当我移动1.jpg.myPic目录,我扫描旧的路径1.jpg什么也没发生.logcat说这个文件不存在.所以MediaScannerConnection只将文件添加到图库.如果我传递的新路径1.jpgMediaScannerConnection?以及它增加了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();
}
Run Code Online (Sandbox Code Playgroud)

但它也不起作用,我的文件的缩略图仍然显示在厨房.所以我用getContentResolver()错了方法?这可能是所有Exclusive方法,用于删除文件缩略图显示在库中的情况.我有我的文件路径,我只需要从媒体商店内容提供商删除它.

更新: 事实证明Uri.parse(path)在第三个解决方案中使用是错误的.图像Uri是从content://它开始的,它可以被检索MediaScannerConnection.所以我创建了一个Uri被调用的imageInGalleryUri赋值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;
            }
            });
        }
Run Code Online (Sandbox Code Playgroud)

我尝试了代码,但它只检测第一个图像并从库中删除它,但不影响其他图像.我无法弄清楚为什么.任何的想法?

提前谢谢你的帮助

Yev*_*lik 1

。之前的文件夹只是让它不可见。但有一种说法是根本不要使用此文件夹来画廊。请尝试将名为“.nomedia”的空文件放入您的文件夹中。