Here is my code:
File storageFile = new File("/mnt/extSdCard/DCIM/Camera/IMG_123456789.jpg");
if(storageFile.exists()) {
//copy the file to another folder
MyCopyFoo(storageFile);
if(storageFile.delete()) {
Log.d("Debug", "Success!");//have shown
//refresh sth
}
}
After operation, I checked the system gallery, and there is still a thumbnail in it.
When I restarted the system, it was gone.
I know there is some other way to handle this- the "setting"=>clear sth
What if I wanna deal with it in the code above?
Use MediaScannerConnection to notify the system of modified media files and to regenerate their metadata and thumbnails.
Mediascanner runs as part of the boot sequence so that's why a reboot also fixes the issue.
Related
I am developing an app which has some features for image handling too. For that I am creating a directory in the device's internal storage with path /CamScan/tempFiles in which I am storing images for a temporary basis while app is running. On back press when app is exiting, and also when app runs again, I am calling a deleteRecursive() method to delete the complete tempFiles directory (if it exists).
This is my deleteRecursive() method:
public void deleteRecursive(File file) {
try {
if (file.isDirectory()) {
for (File child : file.listFiles())
{
deleteRecursive(child);
}
}
file.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
And this is how I am calling it onBackPressed and onCreateView:
File directory = new File(Environment.getExternalStorageDirectory(),"/CamScan/tempFiles");
deleteRecursive(directory);
It is working fine for deleting the directory and its child on exit but after exiting the app I can still see a folder with name tempFiles in my gallery that has actually empty image placeholders (that complete black background which keeps loading if I click on it). If I navigate to my app's directory CamScan, I don't see any directory with name tempFiles but it exists (with some empty placeholders) in the gallery.
In my app I have a grid view in which I am loading all the gallery images. These empty placeholders exists even there (its obvious as I am using MediaStore to get those images) but if select that placeholder and try to do any operation on it, it throws a FileNotFoundException (which is again obvious as the file does not exist in the storage).
My Question:
What is it that I am doing wrong. How can I avoid such errors, is there any solution to the problem or at-least an alternative to what I am doing.
Thank you
I have a small problem here.
In my application I let the user select a picture from the gallery. I save the path to it before doing anything else.
When the user picks the picture he wants, I want it to be copied in a other folder, and then deleted from the original one.
Well, it kiiinda works. The original picture is deleted, and a copy appears in the other folder.
Buuut. It's still there. The deleted picture can still be seen in the gallery, and the copy can't be seen. When I call Gdx.files.absolute(originalPath).exists() it returns false, and Gdx.files.external(copyPath).exists() it returns true, and I can work with the copy of the picture with no problem.
It looks like the gallery is not updated.
I use this to delete and copy a picture :
public void MoveToCustomFolder() {
if (DoesOriginalPathExist()) {
if (!DoesCopyExist()) {
System.out.println("Copying");
Gdx.files.external("/CustomFolder/" + fileName).write(Gdx.files.absolute(filePath).read(), true);
}
System.out.println("Deleting");
Gdx.files.absolute(filePath).delete();
}
}
filePath being the absolutePath of the original picture in the gallery and fileName the name of the file ("picture.jpg")
I found something during my research. When clear the data of the media storage application, after little time the correct gallery shows up, with no deleted pictures and with copies where they belong.
Also, I do have the WRITE_EXTERNAL_STORAGE permission.
Do you guys know what's wrong ?
Found the solution.
I had to update the Gallery with this function :
public void UpdateGallery(String filePath) {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(filePath))));
}
I have an app which performs processing on an image selected by the user using from the gallery, after processing the modified image is then saved back to the original image's folder with a modified filename. On specific devices (mainly Samsungs) the new KitKat permission limitations severely limit where a non system app can write to e.g. external SD card or back to typical gallery locations.
The documentation advises to write to the private package specific folder obtained via
getExternalFilesDir(null)
This is fine and the image is saved correctly but then a subsequent call to
MediaScannerConnection.scanFile
fails to update the central media database correctly as the modified image doesn't appear in the gallery, even a reboot of the device which should force a full scan of all media doesn't trigger the image to be displayed.
In other words the following code doesn't work:
File appDir = MyActivity.this.getExternalFilesDir(null);
File file = new File(appDir, "new_image.jpeg");
// Process image and write file away...
MediaScannerConnection.scanFile(this, new String[] { file.getAbsolutePath() }, null, new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Log.i(LOG_TAG, String.format("Scanned file: %s", path));
}
});
The onScanCompleted method fires but the gallery doesn't show the new image.
Has anyone seen this behaviour, does MediaScanner not scan 'private' app folders and if so how else do I get the gallery to detect and display the new image.
If you want images to be picked up by the gallery, you should put them in the proper public location. You can get the proper location using Environment.getExternalStoragePublicDirectory().
My code behavior changes at the artwork for a single MP3 file. By opening the MP3 file on my PC the artwork got changed but by opening the MP3 file on the android device nothing happens. The shown artwork is still the old one.
I tried this:
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(song_path);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
FindCoverActivity.this.sendBroadcast(mediaScanIntent);
but nothing happend.
I tried another thing: By changing the artwork AND the name of the album the mediastore immediatly noticed what was going on and by opening the mp3 file on the android phone again, the artwork gets displayed correctly with the new artwork. But it is not my goal to change important mp3 tags like the album name. Could this be a bug or has anyone suggestions?
Thanks!
You could try MediaScannerConnection and its scanFile() method instead of ACTION_MEDIA_SCANNER_SCAN_FILE and see if you have better results.
Also, make sure that the file's last-modified timestamp is changed when you save your modified artwork. I'm not an expert at such MP3 file modifications, but if there is a timestamp tag in there, you might consider updating that timestamp as well. Basically, you need to make sure that the MediaStore realizes that there is a change that requires an update of its metadata.
If all else fails, you could try to work out an approach that temporarily modifies the album name in a minor fashion (e.g., appends a space), then changes it back. This is a hack, but it may be your only reliable option.
My app is using images from folder /storage/emulated/0/Android/data/com.android.providers.media/albumthumbs. When I change language and restart phone it is deleting image files from /storage/emulated/0/Android/data/com.android.providers.media/albumthumbs. Now to recreate image file in that folder I have to launch native media player.
I am using Galacy S4 and issue is frequently happens when I change language to Korean.
Do someone know why it is deleting files on langauge change and restart and which action I can use inside my app to recreate image files in /com.android.providers.media/albumthumb like they are using in native media player.
If I delete all images from /com.android.providers.media/albumthumbs how I can fill it again will images of music files on launch of my app. Like if I launch Google Music of Samsung Music Player images are created in the folder. How I can do that on launch of my app.
Maybe too late but I had the same problem in my app.
A workaround which works for me is following
Firstly I load vie media store all album arts. So I can get some file paths but there is no files therefore for begin I check whether file exists
File f = new File(coverPath);
if(!f.exists()){
}
if not then I do this
public void loadAlbumArtById(long id) {
try {
Uri songCover = Uri.parse("content://media/external/audio/albumart");
Uri uriSongCover = ContentUris.withAppendedId(songCover, id);
ContentResolver res = this.context.getContentResolver();
InputStream in = res.openInputStream(uriSongCover);
} catch (Exception ex) {
// do something
}
}
Where Id is album id from media store (MediaStore.Audio.Albums._ID)
After I run this function, art is available again, I don't know why, but it works for me