officialhopsof
officialhopsof

Reputation: 173

Android WebView Allow Image Upload on m.facebook.com

So I have an Android WebView that is designed to navigate m.facebook.com and I am having trouble getting image uploads working. When I click on the upload image button (on facebook in my WebView) nothing happens.

The button I am pressing on the m.facebook.com website is this one:

Facebook Upload Image

I have defined my WebChromeClient as follows:

_webview.setWebChromeClient(new WebChromeClient() {  
    public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType ) {  
        // BREAKPOINT HERE
        mUploadMessage = uploadMsg;  
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
        i.addCategory(Intent.CATEGORY_OPENABLE);  
        i.setType("image/*");  
        MainActivity.this.startActivityForResult( Intent.createChooser( i, "" ), MainActivity.FILECHOOSER_RESULTCODE ); 
    }

    // For Android < 3.0
    public void openFileChooser( ValueCallback<Uri> uploadMsg ) {
      openFileChooser( uploadMsg, "" );
    }  

    // For Android > 4.1
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
      openFileChooser( uploadMsg, "" );
    }
});

Which I took from Chirag's answer from this previous stack overflow post: Android WebView File Upload

I placed a breakpoint where my "// BREAKPOINT HERE" comment is, and it is never hit, even after pressing the upload photo button on m.facebook.com. I have the READ_EXTERNAL_STORAGE permission set. My minimum api version is 9 and my target is 21.

I also have set a WebViewClient which I define as follows:

private class MyWebViewClient extends WebViewClient {
    Activity activity;

    public MyWebViewClient(Activity myActivity){
        activity = myActivity;

    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url){
        // This function is never called when I click the upload photo
        // button.

        // ... Do Stuff
    }

    public void onLoadResource(WebView view, String url){

        // The following if statement's predicate returns false if the 
        // upload image button is pressed, so it's contents are of no consequence
        if(url.startsWith(LINK_STRING)){
            // ...
        }

    }
}

For completeness I have included a portion of my manifest:

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="21" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data android:name="com.google.android.gms.version"
       android:value="@integer/google_play_services_version"/>

    <activity android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:alwaysRetainTaskState="true"/> <!-- Remove this if this doesn't correct the crashing issue -->

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

My question is this: How can I allow the WebView component upload images from the gallery and camera?

Thank you so much for any help you can offer!

Upvotes: 2

Views: 2142

Answers (1)

caw
caw

Reputation: 31487

You have to extend WebChromeClient and add four methods, as described in the issue that you linked.

You may use this library which does these things for you:

https://github.com/delight-im/Android-AdvancedWebView

... or look at the source code to see how it's done:

https://github.com/delight-im/Android-AdvancedWebView/blob/ecff154ef390a0dbdb5337bd5dea2055205c104f/Source/src/im/delight/android/webview/AdvancedWebView.java#L524

Upvotes: 3

Related Questions