java - How can I upload a files using WebView in Android Lollipop? -


i new android, have developed app of webview, have file-chooser button , it's not working in lollipop.i installed apk in mi5 marshmallow nothing response came

this code , me need include file upload enable options on webview

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/activity_main"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingbottom="0dp"     android:paddingleft="0dp"     android:paddingright="0dp"     android:paddingtop="0dp"     tools:context="com.example.tamil.stagingsite.mainactivity">      <progressbar         android:id="@+id/progressbar"         style="@android:style/widget.material.light.progressbar.large"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerhorizontal="true"         android:layout_centervertical="true" />      <webview         android:id="@+id/activity_main_webview"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_alignparentstart="true"         android:layout_alignparenttop="true"         android:animationcache="true"         android:background="@android:color/white">      </webview> </relativelayout> 

mainactivity.java

package com.example.tamil.stagingsite;  import android.app.activity; import android.graphics.bitmap; import android.net.uri; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.view.window; import android.webkit.websettings; import android.webkit.webview; import android.webkit.webviewclient; import android.widget.progressbar;  import com.google.android.gms.appindexing.action; import com.google.android.gms.appindexing.appindex; import com.google.android.gms.appindexing.thing; import com.google.android.gms.common.api.googleapiclient;  public class mainactivity extends activity {       private webview mwebview;     /**      * attention: auto-generated implement app indexing api.      * see https://g.co/appindexing/androidstudio more information.      */     private googleapiclient client;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         requestwindowfeature(window.feature_no_title);         setcontentview(r.layout.activity_main);          mwebview = (webview) findviewbyid(r.id.activity_main_webview);          websettings websettings = mwebview.getsettings();         websettings.setjavascriptenabled(true);         mwebview.loadurl("http://census-staging.herokuapp.com/");         mwebview.setwebviewclient(new webviewclient());         mwebview.setwebviewclient(new myappwebviewclient());          // attention: auto-generated implement app indexing api.         // see https://g.co/appindexing/androidstudio more information.         client = new googleapiclient.builder(this).addapi(appindex.api).build();     }       @override     public void onbackpressed() {         if (mwebview.cangoback()) {             mwebview.goback();         } else {             super.onbackpressed();         }     }       /**      * attention: auto-generated implement app indexing api.      * see https://g.co/appindexing/androidstudio more information.      */     public action getindexapiaction() {         thing object = new thing.builder()                 .setname("main page") // todo: define title content shown.                 // todo: make sure auto-generated url correct.                 .seturl(uri.parse("http:staging.herokuapp.com/"))                 .build();         return new action.builder(action.type_view)                 .setobject(object)                 .setactionstatus(action.status_type_completed)                 .build();     }      @override     public void onstart() {         super.onstart();          // attention: auto-generated implement app indexing api.         // see https://g.co/appindexing/androidstudio more information.         client.connect();         appindex.appindexapi.start(client, getindexapiaction());     }      @override     public void onstop() {         super.onstop();          // attention: auto-generated implement app indexing api.         // see https://g.co/appindexing/androidstudio more information.         appindex.appindexapi.end(client, getindexapiaction());         client.disconnect();     } } 

use code in setwebchormeclinet.

  webview.setwebchromeclient(new webchromeclient() {          // openfilechooser android 3.0+         public void openfilechooser(valuecallback<uri> uploadmsg, string accepttype){                // update message             muploadmessage = uploadmsg;              try{                      // create androidexamplefolder @ sdcard                  file imagestoragedir = new file(                                        environment.getexternalstoragepublicdirectory(                                        environment.directory_pictures)                                        , "androidexamplefolder");                  if (!imagestoragedir.exists()) {                     // create androidexamplefolder @ sdcard                     imagestoragedir.mkdirs();                 }                  // create camera captured image file path , name                  file file = new file(                                 imagestoragedir + file.separator + "img_"                                 + string.valueof(system.currenttimemillis())                                  + ".jpg");                  mcapturedimageuri = uri.fromfile(file);                   // camera capture image intent                 final intent captureintent = new intent(                                               android.provider.mediastore.action_image_capture);                  captureintent.putextra(mediastore.extra_output, mcapturedimageuri);                  intent = new intent(intent.action_get_content);                  i.addcategory(intent.category_openable);                 i.settype("image/*");                  // create file chooser intent                 intent chooserintent = intent.createchooser(i, "image chooser");                  // set camera intent file chooser                  chooserintent.putextra(intent.extra_initial_intents                                        , new parcelable[] { captureintent });                  // on select image call onactivityresult method of activity                 startactivityforresult(chooserintent, filechooser_resultcode);                }              catch(exception e){                  toast.maketext(getbasecontext(), "exception:"+e,                              toast.length_long).show();              }          }          // openfilechooser android < 3.0         public void openfilechooser(valuecallback<uri> uploadmsg){             openfilechooser(uploadmsg, "");         }          //openfilechooser other android versions         public void openfilechooser(valuecallback<uri> uploadmsg,                                     string accepttype,                                     string capture) {              openfilechooser(uploadmsg, accepttype);         } 

reference link here


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -