Android,无法使用WebView上传图片

标签 android image input webview crop

几天来我一直在为我的问题寻找解决方案,但找不到好的解决方案。所以我想在开始为 Android 开发 native 应用程序之前再次在这里询问,因为我无法解决这个问题。

正如标题所说,我正在尝试通过使用 webview 将图像上传到通常选择图像的 html 内容,将其显示在页面上并允许用户裁剪它。 我制作了整个应用程序并在手机的普通浏览器上对其进行了测试,所以我不知道这行不通。这就是它令人沮丧的原因。

我在互联网上找到的那些解决方案不适用于我的 Android,而且我读到它也不可能了。

有谁知道这是否可能?如果我能获得更多关于此的信息(即使可能与否?),那将是一个很大的帮助。 感谢 Adavnce...

最佳答案

尝试添加这些权限。

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Android 6.0 Marshmallow introduces a new model for handling permissions, which streamlines the process for users when they install and upgrade apps. Provided you're using version 8.1 or later of Google Play services, you can configure your app to target the Android 6.0 Marshmallow SDK and use the new permissions model.

If your app supports the new permissions model, the user does not have to grant any permissions when they install or upgrade the app. Instead, the app must request permissions when it needs them at runtime, and the system shows a dialog to the user asking for the permission.

To learn more, see the documentation for Android 6.0 Marshmallow and the changes you must make to your app for the new permissions model.

Google 添加了 WebChromeClient.onShowFileChooser .他们甚至提供了一种自动生成文件选择器 Intent 的方法,以便它使用输入接受 mime 类型。

以这种方式实现它(来自 an answer by weiyin ):

public class MyWebChromeClient extends WebChromeClient {
        // reference to activity instance. May be unnecessary if your web chrome client is member class.
    private MyActivity myActivity;

    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        // make sure there is no existing message
        if (myActivity.uploadMessage != null) {
            myActivity.uploadMessage.onReceiveValue(null);
            myActivity.uploadMessage = null;
        }

        myActivity.uploadMessage = filePathCallback;

        Intent intent = fileChooserParams.createIntent();
        try {
            myActivity.startActivityForResult(intent, MyActivity.REQUEST_SELECT_FILE);
        } catch (ActivityNotFoundException e) {
            myActivity.uploadMessage = null;
            Toast.makeText(myActivity, "Cannot open file chooser", Toast.LENGTH_LONG).show();
            return false;
        }

        return true;
    }
}


public class MyActivity extends ... {
    public static final int REQUEST_SELECT_FILE = 100;
    public ValueCallback<Uri[]> uploadMessage;

    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == REQUEST_SELECT_FILE) {
                if (uploadMessage == null) return;
                uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
                uploadMessage = null;
            }
        }
    }
}

确保使用 API 21+ 编译应用程序。正如您在 gradle 中提到的那样,这将适用于所有平台。

关于Android,无法使用WebView上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36410012/

相关文章:

java - 确定 blob 在 java 中是否有图像

php - 迭代多个 $_POST 数组

html - 如何使输入字段的不透明度不影响其中的文本颜色?

android - 改变方向时 fragment 膨胀错误

java - 旋转 以度为单位的 View 变换(仅在 x 轴上)

java - 代号来自 url 的一张图片

javascript - CSS 缩放和正方形中心裁剪图像

c - 输入不在c中退出,因此输出不起作用

java - SQLite 数据库查询只返回一行

Android:如何绘制位图的单一颜色?