Upload photo from gallery or take from camera in Webview

蓝咒 提交于 2019-12-06 06:03:35
Vishal Mokal

In your example

wv.setWebViewClient(new WebViewClient() {      
   public boolean shouldOverrideUrlLoading(WebView v, String url) { 
     if (url.startsWith("testzapp:")) {
       //do whatever action you had intended
     }
   }
 }

here is my solution

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


public void onActivityResult(int requestCode, int resultCode,
        final Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == TAKE_PICTURE) {
            try {
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
                Random randomGenerator = new Random();
                randomGenerator.nextInt();
                newimagename = falidi + "_" + randomGenerator.nextInt()
                        + ".jpg";
                File f = new File(Environment.getExternalStorageDirectory()
                        + File.separator + newimagename);
                try {
                    f.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // write the bytes in file

                FileOutputStream fo = null;
                try {
                    fo = new FileOutputStream(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();
                }
                String uri = f.getAbsolutePath();
                // this is the url that where you are saved the
                // image

                File fx = new File(uri);

                Bitmap bitmap = BitmapFactory.decodeFile(fx.getPath());
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 30, stream);
                byte[] byte_arr = stream.toByteArray();
                String image_str = Base64.encodeToString(byte_arr,
                        Base64.DEFAULT);

                client = new DefaultHttpClient();
                HttpPost post = new HttpPost(
                        "http://www.xxx.com/android/library/image.php?name="
                                + newimagename);
                List<NameValuePair> pairs = new ArrayList<NameValuePair>();
                pairs.add(new BasicNameValuePair("image", image_str));
                try {
                    post.setEntity(new UrlEncodedFormEntity(pairs));
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // Candemir
                new ResultTask().execute(new HttpPost[] { post });
                // Candemir

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

        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!