Open camera/file by Webview

Asked

Viewed 1,397 times

3

I made an application to open my responsive site using Webview from Android Studio. Wheel ok, but when I need to send some file through the site, take a photo and save, do not open the camera! Someone’s been through it?

Telaprincipal.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" />

1 answer

2

Permissions are missing in your code.

On Androidmanifest.xml, for example, you should have:

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

Besides the uses-Feature:

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

When declaring 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());
        }
    });
  • What part of the code do I declare these permissions in? From Androidmanifest.xml I’ve already added!

  • Replaces the line view.setWebChromeClient(new Webchromeclient()); // Adding js Alert support for all this code from the Java part

  • Make a mistake on the line L.d("onPermissionRequest");&#xA; request.grant(request.getResources());&#xA;

  • @Expl0it you managed to hit the line error onPermissionRequest?

  • @Leonardodias how to solve this mistake of onPermissionRequest, of setWebChromeClient? Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.