nativescript-camera
nativescript-camera copied to clipboard
Pictures are saved to file system but never deleted
I had a problem with my app, where for every picture taken, the storage consumption would grow of about 5 MB, on Android 7.0 (Samsung J5).
Tried debugging, to see if I made a mistake or something, and found out that every picture you take is saved in a folder (path specified with this line)
picturePath = utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath() + "/" + "NSIMG_" + dateStamp + ".jpg";
Wouldn't it be better if the plugin did either:
- Remove the file after consumption
- Have a "filepath" property in the options, in order to write the file directly to the folder you need it to be.
As of now I'm removing the file as soon as I get the ImageSource from ImageAsset.
Hi @andreacappadona17,
I tested the described scenario while using the nativescript-camera
plugin and check the folder, while using the provided path from utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath()
, however the folder was empty. I am attaching the sample application, could you review it and make the needed changes, which will allow us to recreate this problem on our side.
Archive.zip
Hi @tsonevn, have you tried setting "saveToGallery" to "false"?
Hi @andreacappadona17 ,
I tested further and indeed while saveToGallery
to false
the image will be saved in the path returned from the utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath()
. I will mark this as a new feature and the other client could also vote for this feature and leave comments what is the best way fo them to handle this case:
- the plugin to delete the images from the folder at a specific time. The problem here could be related to defining the right time to do this or
- the user manually to handle the case and to delete the content of the folder when the images are no longer needed. In my opinion, the second option will be more applicable. I am attaching sample code, which demonstrates this scenario.
import {Folder} from "tns-core-modules/file-system"
let tmpfolder = Folder.fromPath(utilsModule.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath())
tmpfolder.clear().then(function () {
console.log("Successfully cleared the folder.");
}, function (error) {
console.log("Failed to clear the folder.");
});
Hi @tsonevn,
what about having parameter let the user specify the path the file will be saved to?
That parameter would only be used if saveToGallery
is set to false
, and if path is null
or not valid, the file could still be saved in the path returned from utils.ad.getApplicationContext().getExternalFilesDir(null).getAbsolutePath()
Hi @andreacappadona17, Thank you for your suggestion. This is another option, which will allow handling this case. As I already suggested let's leave this issue open, We will discuss all provided options in this issue and will investigate, what would be the best approach.
I find that takepicture saves a copy at two different locations (apart from the location where I want to save the image), both with different names. This may explain why memory is growing three times the amount you would expect. Besides, bothextra location are public, i.e. visible from outside the app, which may be a problem if you want to keep its content hidden. Removing the two copies poses some problems, also because the file names seem to be independent of each other.
The two locations are:
-
knownFolders.documents().path, e.g. /storage/emulated/0/Android/data/org.nativescript.mappicomponents/files
-
path.join(android.os.Environment.DIRECTORY_DCIM,'Camera'), e.g. /storage/emulated/0/DCIM/Camera/IMG_20181224_142219713.jpg
I removed the first with folder.clear(). This works if the folder is not being used for something else. I have no idea what the function of this folder is and suspect it is a kind of public folder inside the app ?
I removed the second copy by finding the last added file and using file.remove(). Although this clears the image, it leaves an empty place in the gallery which I can only remove by throwing it into trash by hand. I haven't found an automatic solution until now. Ideas ?
A third point to take into account is that images may be automatically saved/synchronized in/with the cloud. It is important to remove the copies before this can happen.
In my case, I take picture, get the image asset and upload it directly to a webservice. I used "saveToGallery : false" believing no image wouls be created on disk but here I found a ton of NSIMG__.jpg Isn't it more a bug than a feature ?
@tsonevn Is it possible to store images in internal storage rather than external storage?
@tsonevn Any updates on my issue?