i'm trying to upload files with my webview and found the code from here File Upload in WebView but im not able to integrate the code with my own webview (force closes whenever i open the app).
heres the java
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Console extends Activity {
private WebView mWebView;
// UPLOAD MANAGER PART-1//
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
@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;
}
}
// *********************//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_console);
CookieManager.getInstance().setAcceptCookie(true);// Enable Cookies
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);// Enable Java Script
mWebView.loadUrl("http://www.google.com/"); // Set Home page
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);// Remove
// ScrollBars
mWebView.getSettings().setDefaultFontSize(12);// Set Font Size
mWebView.getSettings().setLoadsImagesAutomatically(true);// Enable Image
// Loading
mWebView.getSettings().setPluginState(PluginState.ON);// Enable Flash
mWebView.getSettings().setRenderPriority(RenderPriority.HIGH); // improves
// Feedback
// on
// touch
// mWebView.setBackgroundColor(0x00000000);//Transparent Screen When
// Loading
// mWebView.getSettings().setBuiltInZoomControls(true);//Set Zoom
// Controls
// mWebView.getSettings().setDisplayZoomControls(false);//Always Hide
// Zoom Controlls(Requires Api 11)
mWebView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);// Set Cache
// (8mb)
String appCachePath = getApplicationContext().getCacheDir()
.getAbsolutePath();// Set Cache (8mb)
mWebView.getSettings().setAppCachePath(appCachePath);// Set Cache (8mb)
mWebView.getSettings().setAllowFileAccess(true);// Set Cache (8mb)
mWebView.getSettings().setAppCacheEnabled(true);// Set Cache (8mb)
mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);// Set
// Cache
// (8mb)
mWebView.requestFocus(View.FOCUS_DOWN);// Enable WebView Interaction
// mWebView.setWebViewClient(new WebViewClient() {//Open URL on Error
// public void onReceivedError(WebView view, int errorCode, String
// description, String failingUrl) {//Open URL on Error
// mWebView.loadUrl("http://www.google.com");//Open URL on Error
// mWebView.loadUrl("file:///android_asset/error_404.jpg"); //Show
// Offline HTML file or Image on Error
// }
// });
mWebView.setWebViewClient(new WebViewClient());
// UPLOAD MANAGER PART-2//
mWebView.setWebChromeClient(new WebChromeClient() {
// 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("file/*");
Console.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("*/*");
Console.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("FILe/*");
Console.this.startActivityForResult(
Intent.createChooser(i, "File Chooser"),
MainActivity.FILECHOOSER_RESULTCODE);
}
});
setContentView(mWebView);
}
// *********************//
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url) {
webview.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
here is the log cat-
11-16 08:21:40.968: E/AndroidRuntime(30304): FATAL EXCEPTION: main
11-16 08:21:40.968: E/AndroidRuntime(30304): Process: com.example.manage.airpush.com, PID: 30304
11-16 08:21:40.968: E/AndroidRuntime(30304): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.manage.airpush.com/com.example.manage.airpush.com.Console}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
11-16 08:21:40.968: E/AndroidRuntime(30304): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
11-16 08:21:40.968: E/AndroidRuntime(30304): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
11-16 08:21:40.968: E/AndroidRuntime(30304): at android.app.ActivityThread.access$800(ActivityThread.java:163)
11-16 08:21:40.968: E/AndroidRuntime(30304): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
11-16 08:21:40.968: E/AndroidRuntime(30304): at android.os.Handler.dispatchMessage(Handler.java:102)
11-16 08:21:40.968: E/AndroidRuntime(30304): at android.os.Looper.loop(Looper.java:157)
11-16 08:21:40.968: E/AndroidRuntime(30304): at android.app.ActivityThread.main(ActivityThread.java:5335)
11-16 08:21:40.968: E/AndroidRuntime(30304): at java.lang.reflect.Method.invokeNative(Native Method)
11-16 08:21:40.968: E/AndroidRuntime(30304): at java.lang.reflect.Method.invoke(Method.java:515)
11-16 08:21:40.968: E/AndroidRuntime(30304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-16 08:21:40.968: E/AndroidRuntime(30304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-16 08:21:40.968: E/AndroidRuntime(30304): at dalvik.system.NativeStart.main(Native Method)
11-16 08:21:40.968: E/AndroidRuntime(30304): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
11-16 08:21:40.968: E/AndroidRuntime(30304): at android.view.ViewGroup.addViewInner(ViewGroup.java:3772)
11-16 08:21:40.968: E/AndroidRuntime(30304): at android.view.ViewGroup.addView(ViewGroup.java:3621)
11-16 08:21:40.968: E/AndroidRuntime(30304): at android.view.ViewGroup.addView(ViewGroup.java:3597)
11-16 08:21:40.968: E/AndroidRuntime(30304): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:359)
11-16 08:21:40.968: E/AndroidRuntime(30304): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:349)
11-16 08:21:40.968: E/AndroidRuntime(30304): at android.app.Activity.setContentView(Activity.java:1993)
11-16 08:21:40.968: E/AndroidRuntime(30304): at com.example.manage.airpush.com.Console.onCreate(Console.java:131)
11-16 08:21:40.968: E/AndroidRuntime(30304): at android.app.Activity.performCreate(Activity.java:5389)
11-16 08:21:40.968: E/AndroidRuntime(30304): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
11-16 08:21:40.968: E/AndroidRuntime(30304): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
11-16 08:21:40.968: E/AndroidRuntime(30304): ... 11 more