How to make upload button work in android app?

房东的猫 提交于 2019-12-02 14:59:31

问题


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)...

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

I have tried: http://m0s-programming.blogspot.in/2011/02/file-upload-in-through-webview-on.html

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

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

Can someone help me on this?


回答1:


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

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

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



回答2:


some devices upload button not active to use like Samsung s4 and note 3



来源:https://stackoverflow.com/questions/15442843/how-to-make-upload-button-work-in-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!