not able to read input file content using phonegap - android

i am using phone gap to read input file content as follows
<input type="file" id="sendFile" class="ui-input-text ui-body-c">
in my java script i am reading it as follows , this function gets called once the
device is ready
function readImage() {
var fileInput = document.getElementById('sendFile');
var tfile = fileInput.files[0];
var FR= new FileReader();
FR.onloadend = function(e) {
console.log("success");
}
FR.readAsDataURL(tfile);
}
and i am getting following error , i have no idea why its there and how to resolve it
file:///android_asset/www/cordova.js: Line 1064 : processMessage
failed: Error: TypeError: Result of expression
'this._realReader.readAsDataURL' [undefined] is not a function.
i am using android 2.2.1 version
note**
it works with fileEntry object but it doesnt work with input tag , i hope anyone has a solution for this

I think that the problem is that the browser in android 2.x does not fully implement the html5 file api.
If you look at the attributes of the file object you get from the file input on android 2, some are missing like lastModifiedDate and most of the functions are also not there.
Running the same code in an android 4.2.2 device works perfectly :(
Unfortunatly, to get a file object from the file input, you do not rely on a phonegap function but on the support of the html5 file api of the default browser on your device.
It seems that on android the file api is only supported starting with version 3.x
If you only want to support images, you could use the
Phonegap camera plugin and set Camera.sourceType to Camera.PictureSourceType.PHOTOLIBRARY to select a picture from the album.

Related

Python: push file to Android device pass but the file is empty

I have application that i am testing and i want to push file into my android device (real device)
This is what i have try:
self.driver.push_file('/mnt/sdcard/Pictures/photo.png', r'C:\photo.png')
So this operation is pass and i can see the file on my device but its size is 1kb and when i try to open it i have this message:
Its looks like we dont support this file format
What i am doing wrong ?
Pay attention that using Appium with the Python language, when you call the self.driver.push_file() method, the second parameter is the Contents of file in base64 (and not the path of the file on your machine).
That means that you first have to read the file, convert it to base64 (and decode it using utf-8) and only then pass it to this method:
import base64
...
with open(r'C:\photo.png','rb') as file:
driver.push_file('/mnt/sdcard/Pictures/photo.png',
base64.b64encode(file.read()).decode('utf-8'))
Alternatively, you can simply use the following command (adding just source_path= to your snippet):
self.driver.push_file('/mnt/sdcard/Pictures/photo.png', source_path=r'C:\photo.png')
..since the push_file() method was recently updated to support also source path (see Pull #270):
def push_file(self, destination_path, base64data=None, source_path=None)
I would recommend on the source_path parameter of course :)

Cordova App - Open PDF from Expansion File and convert to Base64 String

I have a cordova app up and running where I have an expansion file that is hosting a lot of pdf documents. I want to be able to open a pdf file from that expansion file and convert the entire file into a base 64 string in order to print that pdf from my app. I am trying to use this cordova plugin: https://github.com/sarahgoldman/cordova-print-pdf-plugin which is great, but there is no explanation on how to actually create the base 64 string.
I have attempted to use
var files = new File([""], "filename.pdf",{type:'application/pdf'});
console.log(files);
if (files.length > 0) {
getBase64(files[0]);
}
});
function getBase64(file) {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
console.log(reader.result);
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
}
where the filename.pdf is stored in my expansion file but I don't get a proper response from the code.
any help would be greatly appreciated.
Thank you,
You're really asking two questions in one.
Expansion Files in Cordova
To get at the content of your expansion file, I suggest this cordova plugin:
https://github.com/agamemnus/cordova-plugin-xapkreader/tree/cordova-6.5.0
it facilitates accessing files inside of the expansion file so you can do something like this:
<img src="content://com.test.expansion/myfile.png">
Key is to follow the installation instructions to the point because this plugin requires more work than most of the other ones I've installed.
Base64 Encoding in Javascript
look at the btoa() and atob() functions. They convert text to and from base64 encoding.
Feed your PDF content to the function and hand that over to the pdf plugin.

PhoneGap read XML with FileReader?

I have an XML file on the SDCard, that is downloaded from a server. I am trying to open and read in that XML in order to use it within my app. I open and read it fine, but JQuery is freezing on parsing it.
Once I get the file I do like so:
var reader = new FileReader();
reader.onloadend = function (evt) {
parseXML(evt.target.result);
};
reader.readAsText(file);
And then I begin parsing like so:
function parseXML(xml){
$(xml).find('recipe').each(function(){
And it stops there. If I load the in-app XML using JQuery's ajax method it works fine. So, is there anything special about loading XML data using the FileReader in PhoneGap?
UPDATE:
OK, with much time and help from another dev, the issue appears to be a bug in PhoneGap. Specifically the FileTRansfer object.
I was doing this:
var ft = new FileTransfer();
var dlPath = dataDir.fullPath + "/recipes.xml";
ft.download("http://design.mydomain.com/dave/humana/recipes/recipes.xml", dlPath, addedNewRecipes, addError);
And I'd get a trace within addedNewRecipes that the download succeeded. And it did... just not all of it. The file was truncated and that's what was causing the parseXML to fail. The entire XML file is about 90KB (~1600 lines) it was being truncated at maybe 50 lines.
Replaced FileTransfer with a FileWriter object and it works now.
PhoneGap/Cordova really is still beta isn't it.

Flex Air Mobile File Download

I am building an Adobe Air Mobile application currently targeting Android (and eventually iOS). I'm using Flash Builder 4.6 (which is Flex).
I have a requirement to download a zip file from a url, save it to local storage and unzip the contents.
There seem to be a few example on how to do a download in Flex/Air, but not many in Flex/Air/Mobile. Additionally, the mix of examples seem to be all over the place with namespace versions, etc.
Could someone please provide a succinct example on how to do this in an Air Mobile app?
Thank you!
Edit: What I've tried:
So, from the examples on the web, the first thing I did was add namespace in Flex declarations: xmlns:net="flash.net.*"
Then I added the following component: <net:URLLoader id="urlLoader" />
Then, in the button click event, I called it: urlLoader.load(new URLRequest(downloadUrl));
I get a run time error: Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: http://...
The problem is I don't know where to go from here since I'm not sure what's missing.
this code works for me on IOS:
public function download():void{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
Alert.show('error IO');
});
loader.addEventListener(Event.COMPLETE,downloadComplete);
loader.load(new URLRequest(YOUR_URL));
}
private function downloadComplete(event:Event):void{
try{
var file:File=File.applicationStorageDirectory.resolvePath(YOUR_LOCAL_PATH);
var ba:ByteArray = event.target.data as ByteArray;
var fileStream:FileStream = new FileStream();
fileStream.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
Alert.show('error IO');
});
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(ba);
fileStream.addEventListener(Event.CLOSE, fileClosed);
fileStream.close();
Alert.show("File downloaded succesfully")
}
catch(eeee){
Alert.show("error")
}
}

AS3 Air On Android - Load SWF as its own Main()

I'm using a wrapper app to download a remote SWF and save it to a Xoom Tablet. The download and save works fine but using something like
var file:File = File.applicationStorageDirectory.resolvePath("app.swf");
var loader:Loader = new Loader();
loader.load(new URLRequest(file.url)/*,context needed?*/);
_main.addChild(loader);
Screws with the targeting in the loaded swf so that in my loaded swf if I try and call "stage" from "Main" its null. I thought since loaded SWF's had there own sandbox it would load and run just like it I installed it by its self, does not seem to be the case. If this was a desktop app I could use NativeWindow but its not supported on mobile devices yet. Are there any know options where I can load and run it in its own sandbox where paths and roots are intact? Possibly a command it launch it as a separate swf from my wrapper swf?
I ran into more issues but finaly found the correct way, you cant acceess "File" but this works just fine.
function getSWF():void
{
var request:URLRequest = new URLRequest("child.swf");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadHandler);
loader.load(request);
_main.addChild(loader);// _main is my display sprite
}
function loadHandler(event:Event):void
{
var childSwf:Object = event.target.content;
childSwf.init( PARAMS );// in my case I needed to pass XML but this is any method in the Main of the child swf, note that "stage" is read only, so your parent swf needs to set any stage vars you need
}

Categories

Resources