如何在Android中使用Webview并上传文件

时间:2015-01-22 11:45:47

标签: android webview

对不起,这可能看起来有些奇怪,或者可能是之前可能会提出的问题。但为了这个缘故,请耐心等待。

我是Android开发新手,我为自己的社交网站创建了一个webview应用程序。

  1. 但是当有人试图上传文件时,它什么都不做。
  2. 是否可以在webview中使用谷歌浏览器设置打开网页,以便维护我的应用程序的CSS设置?
  3. 这是我的代码

    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;
        }
    }}
    

1 个答案:

答案 0 :(得分:0)

您可以在这篇文章中找到答案File Upload in WebView

您只需添加此代码。

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 );

       }

});