2

I am sorry this might look a little bit odd or may be the question might have been asked before. But please bear with me for this sake.

I am new to android development and I have created a webview app for my social network site.

  1. But when someone tries to upload a file it doesnt do anything.
  2. Is it possible to open the web with google chrome settings in webview so as to maintain the css settings of my app?

Here are my codes

main xml

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

    <TextView android:text="CBE Social Android Beta Version"
    android:id="@+id/textView1"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:textStyle="bold"
    android:textSize="12px"
    android:textColor="#ff1ecfff"
    android:layout_gravity="center_horizontal">
        </TextView>
    </TableRow>
    <WebView
    android:id="@+id/webview"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:focusableInTouchMode="true">
</WebView>
</LinearLayout>

java

package com.template.WebViewTemplate;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewTemplate extends Activity {
private WebView myWebView;
/** Called when the activity is first created. */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) { //         Enables browsing to previous pages with the hardware back button
        myWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myWebView = (WebView) findViewById(R.id.webview); // Create an instance of WebView and set it to the layout component created with id webview in main.xml
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadUrl("http://www.cobesodom.ac.tz"); // Specify the URL to load when the application starts
    //myWebView.loadUrl("file://sdcard/"); // Specify a local file to load when the application starts. Will only load file types WebView supports
    myWebView.setWebViewClient(new WebViewKeep());
    myWebView.setInitialScale(1); // Set the initial zoom scale
    myWebView.getSettings().setBuiltInZoomControls(true); // Initialize zoom controls for your WebView component
    myWebView.getSettings().setUseWideViewPort(true); // Initializes double-tap zoom control

}

private class WebViewKeep extends WebViewClient {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}}
Jarida Huru
  • 19
  • 1
  • 3
  • possible duplicate of [File Upload in WebView](http://stackoverflow.com/questions/5907369/file-upload-in-webview) – duggu Jan 22 '15 at 11:54

1 Answers1

0

You can found an answer in this post File Upload in WebView

You just need to add this code.

myWebView.loadUrl("http://www.script-tutorials.com/demos/199/index.html");
myWebView.setWebViewClient(new myWebClient());
myWebView.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/*");  
        MyWb.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("*/*");
       MyWb.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/*");  
           MyWb.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MyWb.FILECHOOSER_RESULTCODE );

       }

});
Community
  • 1
  • 1
AlonsoFloo
  • 505
  • 3
  • 10