0

这个问题多次遇到,但似乎没有任何解释有效。(或者也许我在这个叫做互联网的混乱中没有找到它)......

我正在开发一个 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()

有人可以帮我吗?

4

2 回答 2

5

此处回答了有关文件上传的类似问题: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;
        }
    }
}
于 2013-03-16T20:01:22.343 回答
-2

某些设备的上传按钮无法使用,例如三星 s4 和 note 3

于 2015-02-16T22:38:14.550 回答