首页 > 解决方案 > 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 = 29app.gradle应该可以修复它,但这不起作用!

任何人都可以帮忙吗?

标签: angularnativescript

解决方案


嗨,我在 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;
    }

推荐阅读