Open camera / file through WebView

1

I made an application to open my responsive site using the Android Studio WebView. It runs okay, but when I need to send some files through the site, take a picture and save, it does not open the camera! Has anyone gone through this?

MainPage.java

package strikebrasil.sistemastkbr;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class TelaPrincipal extends ActionBarActivity{

private WebView view;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.telaprincipal);


    view = (WebView) this.findViewById(R.id.webView);
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new MyBrowser());
    view.loadUrl("https://strikebrasil.gmpe.com.br/login"); //try js alert
    view.setWebChromeClient(new WebChromeClient()); // adding js alert support

    /*view.getSettings().setJavaScriptEnabled(true);*/
    view.getSettings().setSaveFormData(true);
    view.getSettings().setBuiltInZoomControls(true);
    view.setWebViewClient(new WebViewClient());

    class MyWebViewClient extends WebViewClient
    {
        @Override
        //show the web page in webview but not in web browser
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl (url);
            return true;
        }
    }
}


private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url){
        view.loadUrl(url);
        return true;
    }
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
    //ketika disentuh tombol back
    if ((keyCode == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
        view.goBack(); //method goback() dieksekusi untuk kembali pada halaman sebelumnya
        return true;
    }
    // Jika tidak ada history (Halaman yang sebelumnya dibuka)
    // maka akan keluar dari activity
    return super.onKeyDown(keyCode, event);
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

                   

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.NoActionBar">

    <activity android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".TelaPrincipal">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />


        </intent-filter>
    </activity>

</application>

telaprincipal.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/webView"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

    
asked by anonymous 30.09.2016 / 15:54

1 answer

0

Missing permissions on your code.

In AndroidManifest.xml, for example, you should have:

<uses-permission android:name="android.permission.CAMERA" />

Beyond uses-feature:

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />

At the time of declaring the webView in Java, you must also have some permissions:

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

webview.setWebChromeClient(new WebChromeClient(){
        // Need to accept permissions to use the camera
        @Override
        public void onPermissionRequest(final PermissionRequest request) {
            L.d("onPermissionRequest");
            request.grant(request.getResources());
        }
    });
    
30.09.2016 / 16:00