【问题标题】:Upload photo from gallery or take from camera in Webview从图库上传照片或从 Webview 中的相机拍摄
【发布时间】:2013-07-31 11:13:37
【问题描述】:

我的应用程序是基于网络的,需要上传照片,网站有一个文件输入按钮,我让它与这个一起工作

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

}

但它只显示画廊来挑选照片,我需要同时从相机拍摄。

我尝试了这个解决方案Upload camera photo and filechooser from webview INPUT field,但它只是打开相机,而不是上传拍摄的照片

【问题讨论】:

    标签: android webviewclient webchromeclient


    【解决方案1】:

    在你的例子中

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

    【讨论】:

    • 我现在会检查它**我认为它适用于本机应用程序。我在 webchromeclient 中需要它
    • 制作显示该图像的 div。单击该 div 返回触发 html 中的一些链接/网址。并在 webView Client 中处理该链接。以下事情可能会对您有所帮助 wv.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView v, String url) { if (url.startsWith("testzapp:")) {}
    • 这是我在我的代码中所做的。在 if 条件下,我检查从该链接触发的 url 是否是我想要的,然后我采取相应的操作。在您的情况下,您编写代码以在该条件下获取相机服务,并在您的网页中编写代码以上传该图像
    • 我像你一样做到了。但我现在需要其他方面的帮助.. 在 onActivityResult 中,如何获取在 shouldOverrideUrlLoading 中定义的 url 参数,从 startActivityForResult(intent, TAKE_PICTURE); 开始
    • @Alper:您能告诉我们您是如何解决的吗?我还希望允许用户从 webview 中的相机或图库选项中进行选择。但我得到的只是我们可以使用相机或画廊。你有什么想法在你解决它时如何一起使用它们。提前致谢
    【解决方案2】:

    这是我的解决方案

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多