如何在Android应用程序的上传按钮的工作? [英] How to make upload button work in android app?

查看:155
本文介绍了如何在Android应用程序的上传按钮的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是遇到了好几次,但似乎没有是没有解释的作品。 (也许我没有发现它在这混乱称为Internet)...

This question is encountered several times, though there doesn't seem to be no explanation that works. (or maybe I didn't find it in this chaos called internet)...

我开发一个Android应用程序,打开一个包含上传按钮HTML页面。它不能在web视图工作

I am developing an android app that opens a HTML page that contains an upload button. It doesn't work in WebView.

我曾尝试:的http://m0s-programming.blogspot.in/2011/02/file-upload-in-through-webview-on.html

但日食给出警告,openFileChooser(ValueCallback uploadMsg)从未使用本地。该应用程序应该采用Android 2.2(API 8)及以上。

but eclipse gives warning that "openFileChooser(ValueCallback uploadMsg) is never used locally". The app should work with Android 2.2 (API 8) and above.

这给了一些错误,我猜想因为 WebView.setWebChromeClient错误的位置(新CustomWebChromeClient()

It give some errors, I guess due to wrong placement of WebView.setWebChromeClient(new CustomWebChromeClient()

有人可以帮助我在这?

推荐答案

有关文件上传类似的问题在这里得到了回答:的文件上传中的WebView

A similar question about file upload was answered here: File Upload in WebView.

也有不同版本的Andr​​oid需要不同的方法:<一href="http://stackoverflow.com/posts/12746435/edit">http://stackoverflow.com/posts/12746435/edit

Also different versions of Android require different methods: http://stackoverflow.com/posts/12746435/edit

下面是活动的全面和自给自足code:

Here is full and self-sufficient code of the activity:

public class FileAttachmentActivity extends Activity {

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        WebView wv = new WebView(this);
        wv.setWebViewClient(new WebViewClient());
        wv.setWebChromeClient(new WebChromeClient() {
            //The undocumented magic method override  
            //Eclipse will swear at you if you try to put @Override here  
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                FileAttachmentActivity.this.showAttachmentDialog(uploadMsg);
            }

            // For Android > 3.x
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                FileAttachmentActivity.this.showAttachmentDialog(uploadMsg);
            }

            // For Android > 4.1
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                FileAttachmentActivity.this.showAttachmentDialog(uploadMsg);
            }
        });

        this.setContentView(wv);

        wv.loadUrl("https://dl.dropbox.com/u/8047386/posttest.htm");

    }

    private void showAttachmentDialog(ValueCallback<Uri> uploadMsg) {
        this.mUploadMessage = uploadMsg;

        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");

        this.startActivityForResult(Intent.createChooser(i, "Choose type of attachment"), FILECHOOSER_RESULTCODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == this.mUploadMessage) {
                return;
            }
            Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
            this.mUploadMessage.onReceiveValue(result);
            this.mUploadMessage = null;
        }
    }
}

这篇关于如何在Android应用程序的上传按钮的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆