Mahmoud Ragab
Mahmoud Ragab

Reputation: 67

uploading pictures from website in a webview app

I am working on android app that shows a website only,the website have a button that clicking on it makes you upload a picture.the problem is the app doesn't respond when clicking on the button of the site.

as the arrows shows on clicking on the camera buttton it should show the gallery or camera option to upload a pic to the site.

as the arrows shows on clicking on the camera buttton it should show the gallery or camera option to upload a pic to the site. Here is the code

Main activity

package com.eizeasta.dame;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.graphics.Bitmap;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import com.eizeasta.dame.R;
import com.google.android.gms.ads.AdRequest;

import com.google.android.gms.ads.AdView;


public class MainActivity extends Activity {

    WebView view;
    SwipeRefreshLayout mySwipeRefreshLayout;
    ProgressBar progressBar;
    private WebView browser;
    private ValueCallback<Uri> mUploadMessage;
    private Uri mCapturedImageURI = null;
    private ValueCallback<Uri[]> mFilePathCallback;
    private String mCameraPhotoPath;
    private static final int INPUT_FILE_REQUEST_CODE = 1;
    private static final int FILECHOOSER_RESULTCODE = 1;

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

        findViewById(R.id.imageLoading1).setVisibility(View.VISIBLE);

        AdView adView = (AdView) findViewById(R.id.admob_id);
        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();

        adView.loadAd(adRequest);



        browser = (WebView) findViewById(R.id.activity_main_webview);
        progressBar= (ProgressBar)findViewById(R.id.progressBar);
        final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout);
        final WebView browser = (WebView) findViewById(R.id.activity_main_webview);
        swipeRefreshLayout.setColorSchemeResources(R.color.refresh,R.color.refresh1,R.color.refresh2);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                browser.reload();
                swipeRefreshLayout.setRefreshing(true);

                (new Handler()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipeRefreshLayout.setRefreshing(false);





                    }
                },20000);
            }
        });
        // Force links and redirects to open in the WebView instead of in a browser
        //mWebView.setWebViewClient(new WebViewClient());

        // Enable Javascript
        WebSettings webSettings = browser.getSettings();
        webSettings.setJavaScriptEnabled(true);
        browser.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        browser.getSettings().setAppCacheEnabled(true);
        browser.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        browser.setWebChromeClient(new WebChromeClient());
        browser.getSettings().setAllowFileAccess(true);
        browser.getSettings().setJavaScriptEnabled(true);
        browser.getSettings().setAllowFileAccessFromFileURLs(true);
        browser.getSettings().setAllowContentAccess(true);
        browser.getSettings().setUseWideViewPort(true);
        browser.getSettings().setLoadWithOverviewMode(true);
        browser.getSettings().setAllowFileAccess(true);
        browser.getSettings().setAllowFileAccess(true);
        browser.getSettings().setAllowContentAccess(true);
        // Use remote resource
        browser.loadUrl("https://www.samir.com/forum/_tbdl");

        // Stop local links and redirects from opening in browser instead of WebView
        browser.setWebViewClient(new WebViewClient(){
                                         protected void onActivityResult(int requestCode, int resultCode,
                                                                         Intent intent) {
                                             if (requestCode == FILECHOOSER_RESULTCODE) {
                                                 if (null == mUploadMessage)
                                                     return;
                                                 Uri result = intent == null || resultCode != RESULT_OK ? null
                                                         : intent.getData();
                                                 mUploadMessage.onReceiveValue(result);
                                                 mUploadMessage = null;

                                             }
                                         }
            @Override
                public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
                    try {
                        webView.stopLoading();
                    } catch (Exception e) {
                    }

                    if (webView.canGoBack()) {
                        webView.goBack();
                    }

                    webView.loadUrl("about:blank");
                    AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                    alertDialog.setTitle("Error");
                    alertDialog.setMessage("Check your internet connection and try again.");
                    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                            startActivity(getIntent());
                        }
                    });

                    alertDialog.show();
                    super.onReceivedError(webView, errorCode, description, failingUrl);
                }


            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(final WebView view, String url) {
                findViewById(R.id.imageLoading1).setVisibility(View.GONE);

                progressBar.setVisibility(View.GONE);
                swipeRefreshLayout.setRefreshing(false);

            }
        } );

        // Use local resource
        //mWebView.loadUrl("file:android_asset/web/samir.html");


    }



    // Prevent the back-button from closing the app
    @Override
    public void onBackPressed() {
        if(browser.canGoBack()) {
            browser.goBack();
        } else {
            super.onBackPressed();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



}

And the Andriod manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.eizeasta.dame" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <activity
            android:name="com.eizeasta.dame.MainActivity"
            android:label="@string/app_name"
            android:hardwareAccelerated="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
    </application>
</manifest>

Upvotes: 0

Views: 120

Answers (1)

Alen Smith
Alen Smith

Reputation: 1512

Did you set allow JavaScript on the webview.

 WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

Hope this solves

Upvotes: 0

Related Questions