Skip to content Skip to sidebar Skip to footer

Upload Photo From Gallery Or Take From Camera In Webview

My application is webbased and need to upload photos, website have a file input button, i made it work with this wv = new WebView(this); wv.setWebViewClient(new WebViewClient());

Solution 1:

In your example

wv.setWebViewClient(newWebViewClient() {      
   publicbooleanshouldOverrideUrlLoading(WebView v, String url) { 
     if (url.startsWith("testzapp:")) {
       //do whatever action you had intended
     }
   }
 }

Solution 2:

here is my solution

if (url.startsWith("xxx")) {
                String[] falid = url.split(":");
                falidi = Integer.parseInt(falid[1]);
                Intentintent=newIntent(
                        "android.media.action.IMAGE_CAPTURE");
                startActivityForResult(intent, TAKE_PICTURE);
                returntrue;
            }


publicvoidonActivityResult(int requestCode, int resultCode,
        final Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == TAKE_PICTURE) {
            try {
                Bitmapphoto= (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStreambytes=newByteArrayOutputStream();
                photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
                RandomrandomGenerator=newRandom();
                randomGenerator.nextInt();
                newimagename = falidi + "_" + randomGenerator.nextInt()
                        + ".jpg";
                Filef=newFile(Environment.getExternalStorageDirectory()
                        + File.separator + newimagename);
                try {
                    f.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // write the bytes in fileFileOutputStreamfo=null;
                try {
                    fo = newFileOutputStream(f.getAbsoluteFile());
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    fo.write(bytes.toByteArray());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Stringuri= f.getAbsolutePath();
                // this is the url that where you are saved the// imageFilefx=newFile(uri);

                Bitmapbitmap= BitmapFactory.decodeFile(fx.getPath());
                ByteArrayOutputStreamstream=newByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 30, stream);
                byte[] byte_arr = stream.toByteArray();
                Stringimage_str= Base64.encodeToString(byte_arr,
                        Base64.DEFAULT);

                client = newDefaultHttpClient();
                HttpPostpost=newHttpPost(
                        "http://www.xxx.com/android/library/image.php?name="
                                + newimagename);
                List<NameValuePair> pairs = newArrayList<NameValuePair>();
                pairs.add(newBasicNameValuePair("image", image_str));
                try {
                    post.setEntity(newUrlEncodedFormEntity(pairs));
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // CandemirnewResultTask().execute(newHttpPost[] { post });
                // Candemir

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}

Post a Comment for "Upload Photo From Gallery Or Take From Camera In Webview"