Andreas Gassmann
Andreas Gassmann

Reputation: 6544

Saving image to device and using it in cordova-plugin-file-transfer returns ENCODING_ERR

I am trying to add a feature to an existing cordova plugin. The plugin doesn't allow to return an URI to a specific image, which is what I need. In order to do that, I have to create a local copy of the image. I have never worked with either Java on Android or cordova-plugins in general.

My changes seem to work fine. The file gets created and the plugin returns an URI to that file (in the form of /data/app/cache/image.jpg).

However, when I pass it to the cordova-plugin-file-transfer, I get an error: FileError {code: 5, message: "ENCODING_ERR"}. I found the error code here https://github.com/apache/cordova-plugin-file/#list-of-error-codes-and-meanings, but that doesn't really help me.

This is the code snippet that saves the file:

File outputDir = getContext().getCacheDir();

File outputFile = new File(outputDir, "cdvphotolibrary" + photoNameId);

try {
  BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outputFile));
  bos.write(photo.bytes);
  bos.flush();
  bos.close();
} catch (IOException e) {
  e.printStackTrace();
  callbackContext.error(e.getMessage());
}

callbackContext.success(outputFile.getAbsolutePath());

The full code can be found here: https://github.com/AndreasGassmann/cordova-plugin-photo-library/blob/nativeFileUrls/src/android/PhotoLibrary.java#L226

I read a lot of threads on Stackoverflow about saving images on Android, but they are not specific to Android and some of the solutions (like using ImageIO) don't work.

It would be very helpful if someone could point me in the right direction.

Upvotes: 0

Views: 1517

Answers (1)

Andreas Gassmann
Andreas Gassmann

Reputation: 6544

I found a solution, I simply had to prepend file:// to the string.

So in the end the URI was file:///data/app/cache/image.jpg and the file plugin was able to read it.

Upvotes: 0

Related Questions