3

Fiz um aplicativo para abrir meu site responsivo utilizando o WebView do Android Studio. Roda ok, mas quando preciso enviar algum arquivo pelo site, tirar uma foto e salvar, não abre a câmera! Alguém já passou por isso?

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 Resposta 1

2

Faltam permissões no seu código.

No AndroidManifest.xml, por exemplo, deve ter:

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

Além da uses-feature:

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

Na hora de declarar o webView no Java, também deve ter algumas permissões:

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());
        }
    });
5
  • Em qual parte do código eu declaro essas permissões? Do AndroidManifest.xml eu já adicionei!
    – Expl0it
    Commented 30/09/2016 às 14:23
  • Substitui a linha view.setWebChromeClient(new WebChromeClient()); // adding js alert support por esse código todo da parte do Java Commented 30/09/2016 às 14:31
  • Aprensenta erro na linha ` L.d("onPermissionRequest"); request.grant(request.getResources()); `
    – Expl0it
    Commented 30/09/2016 às 14:48
  • @Expl0it você conseguiu acertar o erro da linha onPermissionRequest?
    – Tiago
    Commented 14/05/2019 às 20:19
  • @LeonardoDias como resolver esse erro do onPermissionRequest, do setWebChromeClient? Obrigado.
    – Tiago
    Commented 14/05/2019 às 20:21

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .