Nativescript 图像选择器无法在外部存储中找到文件

Nativescript Image-picker can't find files in external storage

我使用了 nativescript-imagepicker (related website),并且我完全按照文档和示例代码中的方式实现了所有内容。

我还在 AndroidManifest.xml 中设置了权限代码,该代码适用于 API 29 及更高版本,但不幸的是,我在从设备图库中选择照片后出现此错误:

Asset 'content://com.android.providers.downloads.documents/document/..._photos.jpg' cannot be found.

代码:

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 return 格式的 content:// 图片地址上找了很久才找到你的答案所以图片源找不到 it.you 必须转换 content:// 格式为 file:// format.so 更改下面相同的代码

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