【问题标题】:How to access internal storage while uploading image from webview?从 webview 上传图像时如何访问内部存储?
【发布时间】:2017-10-10 05:04:07
【问题描述】:

我有一个使用 WebView 的应用程序,如果用户想上传图片,我想向他展示我们在 google chrome 中看到的内部存储选项,如图所示

这是我正在使用的代码:

  @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent intent) {
        if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == mUploadMessage)
                return;
            Uri result = intent == null || resultCode != RESULT_OK ? null
                    : intent.getData();
            mUploadMessage.onReceiveValue(result);
            mUploadMessage = null;

        }
    }

    class MyWebChromeClient extends WebChromeClient {

        @Override
        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
           openFileChooser();

            return true;
        }

        // The undocumented magic method override
        // Eclipse will swear at you if you try to put @Override here
        public void openFileChooser() {

            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image");
            MainActivity.this.startActivityForResult(
                    Intent.createChooser(i, "Image Browser"),
                    FILECHOOSER_RESULTCODE);
        }
    }

【问题讨论】:

  • 你试过用i.setType("*/*");替换i.setType("image");吗?
  • i.setType("image/*");这对我有用,谢谢你的努力
  • 在具有 android 4.4.2 操作系统的设备中进行测试。如果它不起作用,请尝试stackoverflow.com/questions/28600165/… this。

标签: android webview image-uploading


【解决方案1】:

尝试以下代码打开文件选择器。

/**
     * Get the Intent for selecting content to be used in an Intent Chooser.
     *
     * @return The intent for opening a file with Intent.createChooser()
     * 
     */
    public static Intent createGetContentIntent() {
        // Implicitly allow the user to select a particular kind of data
        final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        // The MIME data type filter
        intent.setType("*/*");
        // Only return URIs that can be opened with ContentResolver
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        return intent;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多