Friday, 15 April 2011

Android 2.3 webview file upload issue -



Android 2.3 webview file upload issue -

i'm using webview run web application, in app i'm allowing user upload images server, working fine on android > 3, on android 2.3.3 browser working expected on webview file not populated in , server getting empty object. happening when i'm taking image photographic camera (i.e. regular gallery working fine). noticed the uri different 1 file:/// , 1 content:///

attached code: html (my simple test page):

<!doctype html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <form id="fuform" name="fuform" action="/questions/upload" method="post" enctype="multipart/form-data"> <label for="filetoupload">select file upload</label><br /> <input type="file" name="filetoupload" id="filetoupload"/> <input type="submit"/> </form> </body> </html>

mainactivity code:

webview = (webview) findviewbyid(r.id.webview); webview.getsettings().setjavascriptenabled(true); webview.getsettings().setbuiltinzoomcontrols(true); webview.getsettings().setsavepassword(false); webview.getsettings().setsaveformdata(false); webview.setwebviewclient(new mywebviewclient()); webview.setwebchromeclient(new mywebchromeviewclient()); webview.loadurl(url); private class mywebviewclient extends webviewclient { @override public boolean shouldoverrideurlloading(webview view, string url) { view.loadurl(url); homecoming true; } } private class mywebchromeviewclient extends webchromeclient { //@override public void openfilechooser(valuecallback<uri> uploadmsg, string accepttype ) { file imagestoragedir = new file(environment.getexternalstoragepublicdirectory(environment.directory_pictures), "myapp"); // create storage directory if not exist if (! imagestoragedir.exists()){ imagestoragedir.mkdirs(); } file file = new file(imagestoragedir + file.separator + "img_" + string.valueof(system.currenttimemillis()) + ".jpg"); imageuri = uri.fromfile(file); final list<intent> cameraintents = new arraylist<intent>(); final intent captureintent = new intent(android.provider.mediastore.action_image_capture); final packagemanager packagemanager = getpackagemanager(); final list<resolveinfo> listcam = packagemanager.queryintentactivities(captureintent, 0); for(resolveinfo res : listcam) { final string packagename = res.activityinfo.packagename; final intent intent = new intent(captureintent); intent.setcomponent(new componentname(res.activityinfo.packagename, res.activityinfo.name)); intent.setpackage(packagename); intent.putextra(mediastore.extra_output, imageuri); cameraintents.add(intent); } uploadmessage = uploadmsg; intent intent = new intent(intent.action_get_content); intent.addcategory(intent.category_openable); intent.settype("image/*"); intent chooserintent = intent.createchooser(intent,"image chooser"); chooserintent.putextra(intent.extra_initial_intents, cameraintents.toarray(new parcelable[]{})); activity.startactivityforresult(chooserintent, filechooser_resultcode); } // android < 3.0 @suppresswarnings("unused") public void openfilechooser(valuecallback<uri> uploadmsg) { openfilechooser(uploadmsg, ""); } } @override protected void onactivityresult(int requestcode, int resultcode, intent intent) { // super.onactivityresult(requestcode, resultcode, intent); if (resultcode == result_ok && requestcode == filechooser_resultcode) { if (null == uploadmessage) return; final boolean iscamera; if (intent == null) { iscamera = true; } else { final string action = intent.getaction(); if (action == null) { iscamera = false; } else { iscamera = action .equals(android.provider.mediastore.action_image_capture); } } uri result; if (iscamera) { result = imageuri; } else { result = intent == null ? null : intent.getdata(); } uploadmessage.onreceivevalue(result); } else { uploadmessage.onreceivevalue(null); } uploadmessage = null; }

any help appreciated...

joe.

i met same problem, have found solution myself after long time research.

on android 2.3.3, webview doesn’t back upwards uri file protocol (file:///). should convert image’s uri content protocol uri (content:///) before calling uploadmessage.onreceivevalue.the varaiable imagefilepath in below code absolute path of image file captured camera.

@override protected void onactivityresult(int requestcode, int resultcode, intent intent) { // super.onactivityresult(requestcode, resultcode, intent); if (resultcode == result_ok && requestcode == filechooser_resultcode) { if (null == uploadmessage) return; final boolean iscamera; if (intent == null) { iscamera = true; } else { final string action = intent.getaction(); if (action == null) { iscamera = false; } else { iscamera = action.equals(android.provider.mediastore.action_image_capture); } } uri result; if (iscamera) { **string mediauri =mediastore.images.media.insertimage(getcontentresolver(),imagefilepath,"",""); result = uri.parse(mediauri);** } else { result = intent == null ? null : intent.getdata(); } uploadmessage.onreceivevalue(result); } else { uploadmessage.onreceivevalue(null); } uploadmessage = null; }

android file-upload webview

No comments:

Post a Comment