简体   繁体   中英

Delete image thumbnail from gallery when it got hidden

this question has been asked before(not specifically like this) but there hasn't been an All Exclusive answer to it yet. so we are trying to find the best solution here. i'm developing an app and in my app i hide a directory named myPic by moving it's files to a directory called .myPic . when i hide my pictures it's thumbnails are still in gallery. i find 3 solution to this:

first solution:

using ACTION_MEDIA_MOUNTED broad cast like this:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

the problem with this code is that it takes hug resources and most importantly it is blocked since android 4.4 . so using this method is not rational for adding 10 pictures to gallery. so it is not an All exclusive method. also using ACTION_MEDIA_SCANNER_SCAN_FILE doesn't work on android 4.4 either

second solution:

using MediaScannerConnection . so i created a for loop and pass the old address of every file that i hide. this is my MediaScannerConnection function:

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);
            }
        });
}

the thing about MediaScannerConnection is that it only effect if the file exist. so lets say i have a picture called 1.jpg in myPic directory. using this class i can add 1.jpg to my gallery immediately but when i move 1.jpg to .myPic directory and i scan the old path of 1.jpg nothing happen. logcat says that this file doen't exsit. so MediaScannerConnection only add files to gallery. what if i pass the new path of 1.jpg to MediaScannerConnection ? well it adds 1.jpg from .myPic directory to gallery and that is exactly not what i want. so again not an All Exclusive method

third solution:

using getContentResolver() . so for deleting thumbnails this method may be the ultimate solution. so i write the blow code. in every loop i retrieve the path of image and pass it to getContentResolver().delete(Uri.parse(path),null,null) . here is the code:

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();
}

but it's not working either and thumbnails of my files are still showed in galley. so am i using getContentResolver() in wrong way?? this might be the all Exclusive method for the situation where deleted files thumbnails show up in gallery. i have my files path and i need to only delete it from media store content provider.

update: so turns out that using Uri.parse(path) in the third solution is wrong. image Uri is started with content:// and it can be retrieved by MediaScannerConnection . so i created a Uri called imageInGalleryUri and assign null value to it. using my scanFile function i changed it's value from time to time and pass it's value to getContentResolver() . here is the code:

    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;
            }
            });
        }

i tried the code but it only detect the first image and delete it from the gallery but does not effect the other images. i can't figure out why. any idea?

thank you for your help in advance

. before folder just make it invisible. But there is way to say don't use this folder at all to gallery. Please try to put empty file called ".nomedia" file into your folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM