项目作者: AnthonyWard

项目描述 :
高级语言: CSS
项目地址: git://github.com/AnthonyWard/cordova-file-plugin-bug.git
创建时间: 2018-02-25T22:28:57Z
项目社区:https://github.com/AnthonyWard/cordova-file-plugin-bug

开源协议:

下载


Cordova File Plugin Bug (Android)

To Recreeate

  • Run cordova prepare android
  • Run cordova build android
  • Open in android studio
  • See console output (via Chrome Remote Debug or Logcat)

The file is not read correctly, it is much longer than it should be.

The problem

CordovaResourceApi.java method OpenForReadResult return a -1 length to Filesystem.java method readFileAtURL causing file corruption as each chunk effectively has no end.

Fix

Possible fix in the plugin cordova-plugin-file:

https://github.com/apache/cordova-plugin-file/pull/217
https://issues.apache.org/jira/browse/CB-13245?jql=text%20~%20%22CordovaResourceApi%22

Or should it be fixed upstream in cordova-android here:

https://github.com/apache/cordova-android/blob/master/framework/src/org/apache/cordova/CordovaResourceApi.java

In the method OpenForReadResult there is one path (in the catch) that leaves the length as -1 causing the defect

  1. case URI_TYPE_ASSET: {
  2. String assetPath = uri.getPath().substring(15);
  3. AssetFileDescriptor assetFd = null;
  4. InputStream inputStream;
  5. long length = -1;
  6. try {
  7. assetFd = assetManager.openFd(assetPath);
  8. inputStream = assetFd.createInputStream();
  9. length = assetFd.getLength();
  10. } catch (FileNotFoundException e) {
  11. // Will occur if the file is compressed.
  12. inputStream = assetManager.open(assetPath);
  13. }
  14. String mimeType = getMimeTypeFromPath(assetPath);
  15. return new OpenForReadResult(uri, inputStream, mimeType, length, assetFd);
  16. }

Adding length = inputStream.available(); in the catch fixes the issue for me, but I’m not a java developer so can’t evaluate if that’s a bad idea for another reason.