在 Webview 中为输入类型文件打开相机而不打开 Android [英] Open camera for input type file in Webview not opening Android

查看:34
本文介绍了在 Webview 中为输入类型文件打开相机而不打开 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是安卓新手.我正在创建一个小应用程序来拍照并将其上传到服务器.我正在使用 Webview 和 html 表单来做到这一点.在浏览器 (chrome) 中,相机正在打开.但是在 webview 中,单击选择文件"时不会打开相机.有人知道解决方案吗?下面是我使用的代码....

I a newbie in android. I am creating a small app to take photo and upload it to server. I am using Webview and html form do that. In browser (chrome) camera is opening. But in webview, camera is not opened when "choose file" is clicked. Anyone know a solution? Below is the code i used....

HTML 代码

<html>
<body>
<form>
<input type="file" accept="image/*;capture=camera"/>
<input type="submit"/>
</form>
</body>
</html>

Android 中使用的 Java 代码(eclipse)

Java code used in Android (eclipse)

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClient());
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("http://www.inforge.in/thejus/test.html");
}       


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

推荐答案

试试这个:

webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginState(WebSettings.PluginState.ON);
webview.getSettings().setMediaPlaybackRequiresUserGesture(false);

webview.setWebChromeClient(new WebChromeClient(){
    // Need to accept permissions to use the camera
    @Override
    public void onPermissionRequest(final PermissionRequest request) {
        Log.d("onPermissionRequest");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            request.grant(request.getResources());
        }
    }
});

这篇关于在 Webview 中为输入类型文件打开相机而不打开 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆