nativescript - 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 上需要相同的功能。
注意:如果我尝试从库中打开文件,我会收到一条消息,指出图像无法显示,然后它会从视图中消失。问题是删除文件后刷新库。
解决方案
您可以为给定路径手动触发媒体扫描仪。这将反映这些变化。这是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);
推荐阅读
- vba - 带有图片框和计时器的 Visual Basic 循环图像
- javascript - 带有延迟加载模块的 Angular 5 路由器不起作用
- apache-spark - 如何访问流式查询的指标?
- cordova - 安装 Ionic cordova in-app-browser 插件后找不到 Ionic-app-scripts 错误
- css - 我可以在不移动内容的情况下移动“溢出隐藏”框吗?
- ios - json 数据格式不正确的问题
- vue.js - V-once 是否也适用于所应用元素的子节点?
- python - 获取区间的 pandas dtype 表示
- sql - Oracle SQL:如何连接包含所有/通配符值的表?
- android - 带有工具栏的沉浸式模式