Nativescript从库中删除图像

时间:2018-06-22 18:30:27

标签: nativescript

我有一个带TS应用程序的NS 4.1。在其中,我允许用户使用nativescript-imagepicker选择图像。

如果用户选择这样做,我想从设备中删除图像。我正在使用以下代码:

//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();
});

运行代码时,该文件实际上已删除,但仍显示在库中。我还需要怎么做才能将其从媒体库中删除?

当前在Android上进行测试,但在iOS上需要相同的功能。

注意:如果尝试从库中打开文件,则会收到一条消息,提示无法显示图像,然后图像从视图中消失。问题是如何在文件删除后刷新库。

1 个答案:

答案 0 :(得分:0)

您可以手动触发给定路径的介质扫描仪。这将反映出变化。 这是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);

或者您也可以在下面进行

    android.media.MediaScannerConnection.scanFile(application.android.context, [picturePath], null, callback);