cordova camera copyFile错误代码1000

时间:2015-10-08 17:35:39

标签: android ionic-framework android-camera ionic cordova-plugins

如果我使用cordova插件cordova-plugin-file 3.0.0(android)从我的照片库中选择一个图像,我得到了错误代码1000。我不知道错误代码1000测量什么。 如果我拍一张新照片,一切正常。

有人有想法吗?

function optionsForType(type) {
    var source;
    switch (type) {
      case 0:
        source = Camera.PictureSourceType.CAMERA;
        break;
      case 1:
        source = Camera.PictureSourceType.PHOTOLIBRARY;
        break;
    }
    return {
      destinationType: Camera.DestinationType.FILE_URI,
      sourceType: source,
      allowEdit: false,
      encodingType: Camera.EncodingType.JPEG,
      popoverOptions: CameraPopoverOptions,
      saveToPhotoAlbum: false
    };
  }

function saveMedia(type) {
    var deferred = $q.defer();
    var options = optionsForType(type);

    $cordovaCamera.getPicture(options).then(function(imageUrl) {
      var name = imageUrl.substr(imageUrl.lastIndexOf('/') + 1);
      var namePath = imageUrl.substr(0, imageUrl.lastIndexOf('/') + 1);
      var newName = makeid() + name;
      console.log(newName, cordova.file.dataDirectory);
      $cordovaFile.copyFile(namePath, name, cordova.file.dataDirectory, newName)
          .then(function() {
            deferred.resolve(newName);
          }, function(e) {
            // e is error code 1000 
            console.error(e);
            deferred.reject(e);
          });
    });

    return deferred.promise;
  }

1 个答案:

答案 0 :(得分:5)

TL; DR:

问题是:相机插件返回的网址如此content://...,但文件插件似乎只管理这样的网址file:///...

使用此插件转换content://...网址中的file:///...网址:

https://github.com/hiddentao/cordova-plugin-filepath

长版:

错误代码1000表示您可以在UNKNOWN_ERR中看到它:

/plugins/cordova-plugin-file/src/android/FileUtils.java (l.68)

UNKNOW_ERR用于l.585

[...]
} else if(e instanceof JSONException ) {
    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
} else {
    e.printStackTrace();
    callbackContext.error(FileUtils.UNKNOWN_ERR);
}

您是否可以看到此未知错误,打印堆栈跟踪。要查看堆栈跟踪,我使用android监视器。要做到这一点,请转到您的android sdk文件夹并启动监视器,如下所示:

$ ./tools/monitor

对我来说,监视器公开了这个特定的堆栈跟踪:

10-28 20:34:28.645: W/System.err(8846): java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/ from pid=8846, uid=10085 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
10-28 20:34:28.648: W/System.err(8846): at android.os.Parcel.readException(Parcel.java:1540)
10-28 20:34:28.648: W/System.err(8846): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
10-28 20:34:28.649: W/System.err(8846): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
10-28 20:34:28.649: W/System.err(8846): at android.content.ContentProviderProxy.query(ContentProviderNative.java:420)
10-28 20:34:28.649: W/System.err(8846): at android.content.ContentResolver.query(ContentResolver.java:478)
10-28 20:34:28.649: W/System.err(8846): at android.content.ContentResolver.query(ContentResolver.java:422)
10-28 20:34:28.649: W/System.err(8846): at org.apache.cordova.file.ContentFilesystem.openCursorForURL(ContentFilesystem.java:170)
10-28 20:34:28.649: W/System.err(8846): at org.apache.cordova.file.ContentFilesystem.getFileMetadataForLocalURL(ContentFilesystem.java:126)
10-28 20:34:28.650: W/System.err(8846): at org.apache.cordova.file.Filesystem.exists(Filesystem.java:130)
10-28 20:34:28.650: W/System.err(8846): at org.apache.cordova.file.FileUtils.resolveLocalFileSystemURI(FileUtils.java:619)
10-28 20:34:28.650: W/System.err(8846): at org.apache.cordova.file.FileUtils.access$400(FileUtils.java:51)
10-28 20:34:28.650: W/System.err(8846): at org.apache.cordova.file.FileUtils$14.run(FileUtils.java:378)
10-28 20:34:28.650: W/System.err(8846): at org.apache.cordova.file.FileUtils$25.run(FileUtils.java:561)
10-28 20:34:28.650: W/System.err(8846): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-28 20:34:28.650: W/System.err(8846): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-28 20:34:28.650: W/System.err(8846): at java.lang.Thread.run(Thread.java:818)

在谷歌搜索'android.permission.MANAGE_DOCUMENTS + camera + plugin'或类似'FILE_URI + camera + resolveLocalFileSystemURL'后,我发现相机插件返回的URL就像这个content://...但文件插件似乎只管理像这样的网址file:///...

所以,直到这个bug没有修复(如果它真的是一个bug,我不是专家......)你可以使用这个插件来转换content://... URL中的file:///... URL:< / p>

https://github.com/hiddentao/cordova-plugin-filepath