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

Chi*_*rag 0 upload android android-webview

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

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

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

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

它给出了一些错误,我想由于错误的放置 WebView.setWebChromeClient(new CustomWebChromeClient()

有人可以帮我吗?

vor*_*olf 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)