angular - Nativescript Image-picker 在外部存储中找不到文件
问题描述
我使用了 nativescript-imagepicker(相关网站),并且我完全按照文档和示例代码中的方式实现了所有内容。
我还在 AndroidManifest.xml 中设置了权限代码,该代码针对 API 29 及更高版本进行了修复,但不幸的是,从设备图库中选择照片后出现此错误:
找不到资产“content://com.android.providers.downloads.documents/document/..._photos.jpg”。
代码:
let context = imagepicker.create({
mode: "single"
});
context.authorize()
.then(() => {
return context.present();
}).then(selection => {
const imageAsset = selection.length > 0 ? selection[0] : null;
imageAsset.options.height = 1024;
imageAsset.options.width = 1024;
imageAsset.options.keepAspectRatio = true;
ImageSource.fromAsset(imageAsset).then((imageSource: ImageSource) => {
this.defaultImage = "";
this.changedImage = imageSource;
this.uploadImage(imageSource);
}, (err) => {
console.log(err);
})
})
.catch(function (e) {
console.log(e);
});
AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application android:requestLegacyExternalStorage="true" ... >
PS:有人写道,添加compileSdkVersion = 29
到app.gradle应该可以修复它,但这不起作用!
任何人都可以帮忙吗?
解决方案
嗨,我在 search.imagepicker 以content://
格式返回图像地址后找到了你的答案,所以 imagesource 无法找到它。你必须将content://
格式转换为格式。所以file://
在下面更改你的代码
let context = imagepicker.create({
mode: "single"
});
context.authorize()
.then(() => {
return context.present();
}).then(selection => {
const imageAsset = selection.length > 0 ? selection[0] : null;
imageAsset.options.height = 1024;
imageAsset.options.width = 1024;
imageAsset.options.keepAspectRatio = true;
const source = new ImageSource();
if (imageAsset.android) {
//console.log(getPathFromUri(imageAsset.android));
ImageSource.fromFile(getPathFromUri(imageAsset.android)).then((imageSource: ImageSource) => {
this.defaultImage = "";
this.changedImage = imageSource;
this.uploadImage(imageSource);});
//viewModel.uploadFile(file);
}else{
source.fromAsset(imageAsset).then((imageSource: ImageSource) => {
this.defaultImage = "";
this.changedImage = imageSource;
this.uploadImage(imageSource);
})}
}).catch(function (e) {
console.log(e);
});
export function getPathFromUri(path) {
let context=Utils.android.getApplicationContext();
let uri = android.net.Uri.parse(path);
let isKitKat = android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.KITKAT;
// DocumentProvider
// if (isKitKat )
{
// ExternalStorageProvider
if (uri.getAuthority().includes("externalstorage")) {
let docId = android.provider.DocumentsContract.getDocumentId(uri);
let split = docId.split(":");
let type:string = split[0];
//System.out.println("getPath() docId: " + docId + ", split: " + split.length + ", type: " + type);
// This is for checking Main Memory
if ("primary"===type.toLocaleLowerCase()) {
if (split.length > 1) {
return android.os.Environment.getExternalStorageDirectory() + "/" + split[1] + "/";
} else {
return android.os.Environment.getExternalStorageDirectory() + "/";
}
// This is for checking SD Card
} else {
return "/storage/emulated/0" + "/" + docId.replace(":", "/");
}
}
}
return null;
}
推荐阅读
- arguments - 如何使用 CLI 传递第一个参数
- java - 如果与 `read(byte[] b)` 相比,为什么 java.io.InputStream `read()` 方法输出不同的值?
- .net-5 - 当我们将 pfx 导入 Windows 证书存储区时,.Net 5 中是否有办法将私钥存储在 TPM 中?
- r - 无法复制 Choleski 分解 chol2inv 的逆
- c# - DbCommand.Dispose() 上的错误;和错误消息:无法使用已与其底层 RCW 分离的 COM 对象
- javascript - 如何将 Flutter Web 应用程序嵌入网站并通过网站将数据发送到 Flutter Web 应用程序
- javascript - 淡出并移除元素
- python - 如何从 json 文件中打印特定数据?
- r - 在 R 中找到每日系列的最大值和最小值?
- python - 尝试使用 lambda 过滤列表