【问题标题】:How to make upload button work in android app?如何使上传按钮在android应用程序中工作?
【发布时间】:2013-03-04 18:48:27
【问题描述】:

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

我正在开发一个 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) The warning about openFileChooser never used locally is normal, in fact I'm pretty sure it is supposed to give you the warning. If it's not working out for you the problem is somewhere else.
  • @vorrtex 它确实给了我这个警告。但即使忽略这一点,该应用程序实际上在我的模拟器上崩溃了。
  • 我在文章的 cmets 中找到了一个示例应用程序,经过一些小改动后它就可以工作了。 dl.dropbox.com/u/8047386/…。我确定您的应用中有一些不同的东西会崩溃,而上传工作正常。
  • 您将需要添加几个具有不同签名但具有相同主体的函数。看这个问题stackoverflow.com/questions/10953957/…
  • 如果您对我发布的示例有错误,这很奇怪,因为它在我的模拟器上运行。它应该将文件发布到测试站点并显示来自服务器的响应。

标签: android upload android-webview


【解决方案1】:

这里回答了关于文件上传的类似问题: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;
        }
    }
}

【讨论】:

  • @jcesar WebChromeClient 类在 4.4 中没有改变,所以它应该可以继续工作。但我现在无法验证它,我不知道你有什么错误。
  • 完全没有错误,按钮什么都不做,没有日志或错误跟踪。
  • @jcesar 他们更改了 WebView 引擎,这肯定破坏了现有代码。但是您可以尝试将targetSdkversion 参数更改为18,它会像以前一样工作:developer.android.com/guide/webapps/migrating.html
  • 我需要它来工作,这样我才能回到 .NET/C# 的流线型世界。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    相关资源
    最近更新 更多