-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
File.writeFile() refuses to create binary file correctly (png image) ionic 2 #806
Comments
Is this not due to #789? |
I got the same problem when try to use low-level API Your system information:
|
@phattranky which Ionic Native version? |
@ihadeed the latest version But when i try to write a text file, it's working well. Like this
I only got the problem when try to write the blob file. I use the method like @mhartington to convert from Canvas Uri to Blob. Hope my info useful |
When i write the file with ArrayBuffer instead Blob type. It's working well (Both on Lower Level API and Ionic Native File (File.write)). So my temporary solution here is use the below function to write the file as ArrayBuffer type
When use the Blob type. i debug into the cordova-plugin-file, i see the FileReader onload not callback. So i think the problem from there. But don't know why. Then i test the FileReader outside the plugin. I just read my blob file with FileReader, but it not callback to the onload like cordova-plugin-file. Don't know what happen with the FileReader in Angular 2 or Ionic 2. I'm sure the Blob file is correct. Because i converted it to UrlObject and assign to html Img tag, the image show correctly
|
It works fine for me in iOS (10.2.1) but in Android i have the same problem. Using this solution with ArrayBuffer solved the problem in Android. |
I've got the same problem with a pdf file blob. I didn't understand the ArrayBuffer solution, once fileWriter.write or File.writeFile only accepts a string or blob typed parameter. Could someone detail the solution a little more? |
I had similar issues writing Blob - the promise will not resolve. Found out that it was due to Angular's zone issue as described here. The workaround to wrap FileReader did the job for me. |
I have got the same problem while writing the image to device(Android) , understood from the above information that writing as blog make the problem , but the fileWriter method will only accept blob and will never accept Array buffer , can any one explain in brief how to write the image captured from camera to a location in the device ... |
From @shrike71 on November 18, 2016 10:17
Short description of the problem:
File.writeFile() creates a PNG file of 0 bytes when trying to write a Blob made from base64 data
What behavior are you expecting?
Base64 data read from a TEXT column in the database is formatted and converted into a Blob, and saved to the application data directory as a PNG image.
Steps to reproduce:
Detail
I am trying to create a file that consists of base64 data stored in the db. The rendered equivalent of the data is a small anti-aliased graph curve in black on a transparent background (never more that 300 x 320 pixels) that has previously been created and stored from a canvas element. I have independently verified that the stored base64 data is indeed correct by rendering it at one of various base64 encoders/decoders available online.
The development platform is Windows 10, and i've been testing directly on a Samsung Galaxy S7 and S4 so far.
I know that the base64 data has to be converted into binary data (as a Blob) first, as File does not yet support writing base64 directly in to an image file. I found various techniques with which to do this, and the code which seems to suit my needs the most (and reflects a similar way I would have done it in java is illustrated below):
Main code from constructor:
Saves the image with File.writefile():
I have tried dozens of different decoding techniques, but the effect is the same. However, if i hard code simple text data into the writeFile() section, like so:
A text file IS created correctly in the expected application location with the text string above in it. However, I've also noticed that whether the file is the 0 bytes PNG, or the working text file above, in both cases the ".then()" consequence clause of the File Promise never fires.
Helper to convert base64 to Blob:
Additionally, I swapped the above method and used the Ionic 2 native Base64-To-Gallery library to create the images, which worked without a problem. However, having the images in the user's picture gallery or camera roll is not an option for me as I do not wish to risk a user's own pictures while marshalling / packing / transmitting / deleting the data-rendered images. The images should be created and managed as part of the app.
Other information: (e.g. stacktraces, related issues, suggestions how to fix, stackoverflow links, forum links, etc)
User marcus-robinson seems to have experienced a similar issue outlined here, but it was across all file types, and not just binary types as seems to be the case here. Also, the issue seems to have been closed:
ionic-team/ionic-framework#5638
Which Ionic Version? Ionic 2.x
Copied from original issue: ionic-team/ionic-framework#9228
The text was updated successfully, but these errors were encountered: