unknown os platform
Android java tutorial -Webview file upload on Facebook | Whats-online.info
whats-online-info

Android java tutorial -Webview file upload on Facebook


Android
2016:10:27 / Science and Tutorials

Android Java Tutorial: How to build an Android app that uses webview and webchrome client to load Facebook Url, upload and post photo files on Facebook





upload image using webview and webchrome client on Android


Severally times my Android social app got suppressed from the store due to the issue of unable to upload photos on Facebook. However, after thorough research,  about the issue, I finally succeeded solving the bug. 


Image 1: Navigate to the Facebook upload file Page



Image 2: Open the gallery on file chooser select



Image 3: Uploading files to Facebook




Problems encountered on using Android webview client

1. The Android application will not be able to upload and post photos on Facebook

2. On file chooser selected, the app might become unresponsive or might crash


How to solve the above problems.

The above-featured problems can be solved by using both webview client and webchrome client.

This will permit your Android App the ability to load Facebook, upload and post files onto your Facebook account.


Follow the steps mentioned below to unravel the issue.







First, we will create two classes.


MainActivity.java 

This class specifically is our home activity and contains a button that navigates to the WebView class on click event action. We will use Facebook as our example for uploading images using Webview client and Webchrome client.


Upload.java

This class contains our webview and webchrome client to facilitate the loading of our Facebook Url, upload and post files on Facebook.


Note, the below permissions should be allowed and should be placed just before the opening of our application on our AndroidManifest.xml


1. Permissions to read from an external storage - This provides the Android app with permission to access a file from phones storage or an SD Card


2.Permissions for  Internet access -This provides the permission to connect to the Internet.




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



And for our AndroidManifest.XML, include the following code. It contains the permissions and activities for both classes as shown below.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.whatsonline.androidphotouploadonfacebook" >

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

    <uses-sdk android:minSdkVersion="9"
        android:maxSdkVersion="23"></uses-sdk>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".upload"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.whatsonline.androidphotouploadonfacebook.upload" />

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

</manifest>



Four our upload.XML, include the following code as shown below.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ln1">

</LinearLayout>



Upload.Java 

Finally, create Upload.Java Class to carry out the activities as shown in Image 1, Image 2 and Image 3.



package com.whatsonline.androidphotouploadonfacebook; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.content.res.Configuration; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.view.ViewGroup; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.LinearLayout; /** * Created by sada on 6/17/2016. */ public class upload extends Activity { WebView web; private ValueCallback<Uri> mUploadMessage; private final static int FILECHOOSER_RESULTCODE=1; LinearLayout ln1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.upload); web = new WebView(this); web.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); ln1=(LinearLayout) findViewById(R.id.ln1); WebSettings settings=web.getSettings(); settings.setJavaScriptEnabled(true); web.loadUrl("http://www.facebook.com"); web.setWebViewClient(new myWebClient()); web.setWebChromeClient(new WebChromeClient() { //The undocumented magic method override //Eclipse will swear at you if you try to put @Override here // For Android 3.0+ public void openFileChooser(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); upload.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); } // For Android 3.0+ public void openFileChooser(ValueCallback uploadMsg, String acceptType) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("*/*"); upload.this.startActivityForResult( Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE); } //For Android 4.1 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); upload.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), upload.FILECHOOSER_RESULTCODE); } }); ln1.addView(web); } public class myWebClient extends WebViewClient { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); } } //flipscreen not loading again @Override public void onConfigurationChanged(Configuration newConfig){ super.onConfigurationChanged(newConfig); } @Override 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; } } }



These guide will help you solve the bug of uploading files on your android application.


 Download Demo here














You may also like:


Leave a comment












Subscribe to get replies direct to your mailbox

Submit comment