I am using cordova file plugin to delete image from gallery. here is the code
window.resolveLocalFileSystemURL(path+filename, function (result) {
result.remove(function(data){
console.log(data)
});
});
As per above code I am getting success return and file is also deleted from folder. But When I am going to the gallery it is showing me a blank thumbnail. When I am trying to open this file it is showing only grey color. In details it is showing the old path. But in that path file does not exists. I think this is a kind of cache. How can I delete that blank file?
My code was ok, all I need to use Media Scanner to scan the gallery.
Related
This is my question.
First I have create my personal folder on create
File parentFolder = new File(MainApplication.getInstance().getExternalCacheDir().getAbsolutePath()
+ File.separator+"myfolder");
if (!parentFolder.exists()) {
parentFolder.mkdirs();
}
Second in my application,i can receive file such as png from another application,and the file that i received has been saved in /Android/data/packageName/cache/myfolder/hashcode/example.png. And i choose the gallery to open it from intent chooser.
When gallery is open and I can see the png file.I kill my application's process and uninstall it.
Finally I install my application again.The path /Android/data/packageName doesn't been created! And create function show that
MainApplication.getInstance().getExternalCacheDir();
returns null!
Give me some advise! Thank you.
Try using
new Context().getApplicationContext().getExternalCacheDir().getAbsolutePath()
My application downloads a set of images and stores them in the internal storage by creating a folder.
The problem is the images are being shown in the gallery. This should not happen, is there a way to programatically hide the images but still be usable by the app?
Thanks in advance.
EDIT:
This is only happening to the android version of the app. In the iOS it is not showing in the Camera Roll.
This depends where you download the images: If you download them on your Pictures folder or inside of a public folder from sdcard/internal memory then MediaScanner scan them so your pictures will be visible in the Gallery app.
To avoid this you can try one of the following solutions:
Add a .nomedia file inside your destination folder
OR
Download your pictures in app cache directory by using LocalFileSystem.TEMPORARY.
Something like
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fs) {
fs.root.getDirectory("media", {create: true}, function(fileDirectoryEntry) {
// var destFile = fileDirectory + "/" + filename;
// downloadPicture(srcUrl, destFile);
});
});
The downside of this solution is that when the app is removed, your pictures are deleted as well.
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))));
}
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.
One part of my current project is downloading images from URL then saving to SDCard. But the problem is all of saved images in sdcard is displayed in android gallery page. What I want is "I don't want all of my saved images in android gallery."
Add ".nomedia" file. Did not work? Try the second option.
Save your files to a folder that starts with ".", e.g., /sdcard/.myimages/
You can create .nomedia file using this code:
String NOMEDIA=".nomedia";
File Folder = new File(Environment.getExternalStorageDirectory() + "/mydir");
if(Folder.mkdir()) {
nomediaFile = new File(Environment.getExternalStorageDirectory() + "/mydir/"+ NOMEDIA);
if(!nomediaFile.exists()){
nomediaFile.createNewFile();
}
}
I found a simple way(I think so).
Create a folder in 'Android' folder(where data & obb folders are present) and put your media(pics,videos, etc.,) in the folder you've created.
Gallery app ignores that 'Android' folder. Simple :-)
its so late and i know other answers are complete but i think putting .nomedia file makes all pictures in that particular folder hidden :
just change extension of image file programmatically and this way gallery cant detect it as an image . eg if your image is "pic1.jpg" rename it to "pic1.aaa";
and when you want show it again rename it again