2

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());  
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setAllowFileAccess(true);

wv.setWebChromeClient(new WebChromeClient()  
{    
         public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
               mUploadMessage = uploadMsg;  
               Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
               i.addCategory(Intent.CATEGORY_OPENABLE);  
               i.setType("image/*");  
               MainActivity.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MainActivity.FILECHOOSER_RESULTCODE );

}

but it shows just gallery to pick photos, i need to take from camera at the same time.

i tried this solution Upload camera photo and filechooser from webview INPUT field but its only opening camera, not uploading taken photo

Community
  • 1
  • 1
Alper
  • 231
  • 4
  • 11

2 Answers2

0

In your example

wv.setWebViewClient(new WebViewClient() {      
   public boolean shouldOverrideUrlLoading(WebView v, String url) { 
     if (url.startsWith("testzapp:")) {
       //do whatever action you had intended
     }
   }
 }
Michael Alan Huff
  • 3,462
  • 3
  • 28
  • 44
Vishal Mokal
  • 792
  • 1
  • 5
  • 22
  • i will check it now ** i think its for native apps. I need it in webchromeclient – Alper Jul 31 '13 at 11:42
  • make div which show that image. on click of that div return fire some link / url in html. and handle that link in webView Client. Following thing might help you wv.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView v, String url) { if (url.startsWith("testzapp:")) {} – Vishal Mokal Jul 31 '13 at 19:51
  • this is what i have done in my code . in if condition i check if url which is fired from that link is what i want then i take the respective action . in your case you write code to get camera service in that if condition and write the code in your web page to upload that image – Vishal Mokal Aug 01 '13 at 06:06
  • i did it like you. but i need help on something else now.. in onActivityResult how can i get url parameter definated in shouldOverrideUrlLoading, starting with startActivityForResult(intent, TAKE_PICTURE); – Alper Aug 02 '13 at 14:31
  • @Alper: Can you please tell us how you solved it. I also want to allow user to choose from camera or gallery option in webview. But All i got is we can either use camera or gallery . have You any Idea how to use both together as you solved it . thanks in advance – Chirag Nagpal Jan 23 '14 at 10:52
  • @chirag Nandwani I will edit my question with solution tonight – Alper Jan 24 '14 at 15:35
0

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();
            }

        }
    }
}
Alper
  • 231
  • 4
  • 11