首页 > 解决方案 > 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 上需要相同的功能。

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

标签: nativescript

解决方案


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

推荐阅读