Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete image thumbnail from gallery when it got hidden

Tags:

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

like image 453
hadi Avatar asked Apr 24 '15 09:04

hadi


People also ask

What will happen if I delete thumbnails in DCIM?

Thumbnails folder and delete it from the device, which will remove it and any images within it and may save a significant amount of space from the device. However, in doing this there may be issues, including that any current images may no longer display as thumbnails in the Photos gallery.

How do I permanently delete thumbnails?

You can easily delete thumbnail files in your phone by opening file explorer, then go to DCIM folder, then delete the folder . thumbnail. If this folder is not visible in DCIM directory, then enable show hidden files option.

Can I delete Thumbdata in DCIM?

Can I delete THUMBDATA files? If a THUMBDATA file is taking up too much space on your Android device, you can safely delete the file.


1 Answers

. 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.

like image 96
Yevgen Kulik Avatar answered Oct 07 '22 19:10

Yevgen Kulik