Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android Studio中使用WebView上传文件?_Java_Android_Webview_Download - Fatal编程技术网

Java 如何在Android Studio中使用WebView上传文件?

Java 如何在Android Studio中使用WebView上传文件?,java,android,webview,download,Java,Android,Webview,Download,我正在寻找有关此代码的帮助。我需要补充的是,你可以从这个应用程序上传文件。但是我不能让它工作得很好。。。。所以这很有效。我能做的就是下载,而不是上传文件。我能做什么?我把代码留在下面。请明确具体。请考虑仅支持Android 3.0> 我的意思是,这是一个网络视图,当你在网站上时,Facebook点击按钮上传图像,然后打开一个框,让你选择是否在设备中拍照或查看。我想要那个。我能做什么 public class MainActivity extends ActionBarActivity {

我正在寻找有关此代码的帮助。我需要补充的是,你可以从这个应用程序上传文件。但是我不能让它工作得很好。。。。所以这很有效。我能做的就是下载,而不是上传文件。我能做什么?我把代码留在下面。请明确具体。请考虑仅支持Android 3.0>

我的意思是,这是一个网络视图,当你在网站上时,Facebook点击按钮上传图像,然后打开一个框,让你选择是否在设备中拍照或查看。我想要那个。我能做什么

public class MainActivity extends ActionBarActivity {

    private WebView mWebView;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.activity_main_webview);

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("file:///android_asset/www/index.html");
        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());
        // enable local storage
        // enable local storage
        mWebView.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent,
                                        String contentDisposition, String mimetype,
                                        long contentLength) {
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
            }
        });

        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);


        progressBar = (ProgressBar) findViewById(R.id.progressbar);

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int progress) {
                progressBar.setProgress(0);
                progressBar.setVisibility(View.VISIBLE);
                MainActivity.this.setProgress(progress * 1000);

                progressBar.incrementProgressBy(progress);

                if (progress == 100) {
                    progressBar.setVisibility(View.GONE);
                }
            }
        });

    }


    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            // Let the system handle the back button
            super.onBackPressed();

        }
    }


    public class MyAppWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().length() == 0) {
                return false;
            }

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            view.getContext().startActivity(intent);
            return true;
        }

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

你问题的答案是

您将需要实施

1. "openFileChooser" for android>3 and for android >4.1
2. "showAttachmentDialog"
3. "onActivityResult"
这样会弹出一个文件选择器窗口,您可以上传您的文件

这就是我如何以类似的方式在我的工作中使用相同的方法

希望有帮助