【问题标题】:Nativescript Image-picker can't find files in external storageNativescript Image-picker 在外部存储中找不到文件
【发布时间】:2021-09-21 14:46:19
【问题描述】:

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

我还在 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 应该可以解决它,但那不起作用!

有人可以帮忙吗?

【问题讨论】:

    标签: angular nativescript


    【解决方案1】:

    嗨,我在 search.imagepicker 上搜索了很长时间后找到了答案

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

    【讨论】:

      猜你喜欢
      • 2021-10-20
      • 2013-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多