如何使上传按钮在Android应用程序中工作?

时间:2013-03-15 22:06:24

标签: android upload android-webview

这个问题多次遭遇,但似乎没有任何解释可行。 (或许我在这个被称为互联网的混乱中找不到它......)

我正在开发一个Android应用程序,它打开一个包含上传按钮的HTML页面。它在WebView中不起作用。

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

但是eclipse会警告"openFileChooser(ValueCallback uploadMsg) is never used locally"。该应用应适用于Android 2.2(API 8)及更高版本。

由于错误放置WebView.setWebChromeClient(new CustomWebChromeClient()

,我认为会出现一些错误

有人可以帮我吗?

2 个答案:

答案 0 :(得分:5)

有关文件上传的类似问题,请点击此处:File Upload in WebView

Android的不同版本也需要不同的方法:https://stackoverflow.com/posts/12746435/edit

以下是活动的完整且自给自足的代码:

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

答案 1 :(得分:-2)

某些设备上传按钮无法使用,如三星s4和注释3