Nativescript delete image from library

不羁的心 提交于 2021-01-28 09:00:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!