HTML Input Type 'File' Is Not Working on Webview in Android. How to Upload File Using Webview Android

HTML input type 'file' is not working on webview in android. Is there any way to upload file using webview android?

This issue has already been asked and answered here: File Upload in WebView

Also check this:
https://code.google.com/p/android/issues/detail?id=62220

You can use this class: https://github.com/delight-im/Android-AdvancedWebView

File Upload is not working in Android WebView

It's already answered here:
File Upload in WebView

You can also use this webview:
https://github.com/mgks/Android-SmartWebView

How to upload file through Android Webview app?

Try this

     package com.example.filechooser;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.webkit.SslErrorHandler;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

private final static int FILECHOOSER_RESULTCODE = 1;
private ValueCallback<Uri[]> mUploadMessage;

WebView mainWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mainWebView = findViewById(R.id.main_web_view);

mainWebView.setWebViewClient(new MyWebViewClient());

mainWebView.setWebChromeClient(new MyWebChromeClient());

mainWebView.getSettings().setJavaScriptEnabled(true);

mainWebView.loadUrl("https://example.com");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

if (requestCode == FILECHOOSER_RESULTCODE) {

if (null == mUploadMessage || intent == null || resultCode != RESULT_OK) {
return;
}

Uri[] result = null;
String dataString = intent.getDataString();

if (dataString != null) {
result = new Uri[]{ Uri.parse(dataString) };
}

mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}

// ====================
// Web clients classes
// ====================

/**
* Clase para configurar el webview
*/
private class MyWebViewClient extends WebViewClient {

// permite la navegacion dentro del webview
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

/**
* Clase para configurar el chrome client para que nos permita seleccionar archivos
*/
private class MyWebChromeClient extends WebChromeClient {

// maneja la accion de seleccionar archivos
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {

// asegurar que no existan callbacks
if (mUploadMessage != null) {
mUploadMessage.onReceiveValue(null);
}

mUploadMessage = filePathCallback;

Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*"); // set MIME type to filter

MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FILECHOOSER_RESULTCODE );

return true;
}
}

}


Related Topics



Leave a reply



Submit