Nativescript 从库中删除图像 [英] Nativescript delete image from library

查看:44
本文介绍了Nativescript 从库中删除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

目前正在 Android 上进行测试,但需要在 iOS 上使用相同的功能.

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.

推荐答案

您可以手动触发给定路径的媒体扫描器.这将反映变化.这是android的代码

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);

或者你也可以在下面做

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

这篇关于Nativescript 从库中删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆