问题
I have a NS 4.1 with TS application. In it, I am allowing the user to select an image using nativescript-imagepicker.
If the user chosses to do so, I want to remove the image from the device. I am using the following code:
//files is a list of file locations: e.g./storage/emulated/0/DCIM/Camera/IMG_1529637637242.jpg
import * as fs from 'tns-core-modules/file-system';
files.forEach(f => {
let file: fs.File = fs.File.fromPath(f);
if (file)
file.remove();
});
When code runs, the file is actually deleted, but it is still showing in the library. What do I need to do to remove it from the Library as well?
Currently testing on Android, but will need the same functionality on iOS.
NOTE: If I try to open the file from the library I get a message that the image cannot be displayed and then it disappears from the view. The questions is hoiiw to refresh the library after the file is deleted.
回答1:
you can trigger media scanner manually for given path. which will reflect the changes. here is the code for android
android.content.Intent mediaScanIntent = new android.content.Intent(android.content.Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
android.net.Uri contentUri = android.net.Uri.fromFile(imageFile);
mediaScanIntent.setData(contentUri);
application.android.context.sendBroadcast(mediaScanIntent);
or you can also do below
android.media.MediaScannerConnection.scanFile(application.android.context, [picturePath], null, callback);
来源:https://stackoverflow.com/questions/50993790/nativescript-delete-image-from-library