Android,无法使用 WebView 上传图片 [英] Android, can't upload images using WebView

查看:38
本文介绍了Android,无法使用 WebView 上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

有谁知道这是否可能?如果我能得到更多关于这方面的信息(即使可能或不可能?)感谢 Adavnce...

解决方案

尝试添加这些权限.

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

<块引用>

Android 6.0 Marshmallow 引入了新的处理模式权限,简化用户安装过程和升级应用程序.前提是您使用的是 8.1 版或更高版本的 Google播放服务,您可以配置您的应用以针对 Android 6.0Marshmallow SDK 并使用新的权限模型.

如果您的应用支持新的权限模型,则用户没有在他们安装或升级应用程序时授予任何权限.相反,应用程序必须在需要时请求权限运行时,系统会向用户显示一个对话框,询问许可.

要了解更多信息,请参阅 Android 6.0 Marshmallow 和您必须针对新权限模型对应用进行的更改.

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

以这种方式实现它(来自 weiyin 的回答):

<块引用>

public class MyWebChromeClient extends WebChromeClient {//对活动实例的引用.如果您的 web chrome 客户端是成员类,则可能没有必要.私人 MyActivity myActivity;public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {//确保没有现有消息如果(myActivity.uploadMessage != null){myActivity.uploadMessage.onReceiveValue(null);myActivity.uploadMessage = null;}myActivity.uploadMessage = filePathCallback;意图意图 = fileChooserParams.createIntent();尝试 {myActivity.startActivityForResult(intent, MyActivity.REQUEST_SELECT_FILE);} catch (ActivityNotFoundException e) {myActivity.uploadMessage = null;Toast.makeText(myActivity, "无法打开文件选择器", Toast.LENGTH_LONG).show();返回假;}返回真;}}公共类 MyActivity 扩展 ... {public static final int REQUEST_SELECT_FILE = 100;public ValueCallback上传消息;protected void onActivityResult(int requestCode, int resultCode, Intent data){如果(请求代码 == REQUEST_SELECT_FILE){如果(uploadMessage == null)返回;uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));上传消息 = 空;}}}}

确保使用 API 21+ 编译应用程序.这将适用于您在 gradle 中提到的所有平台.

I'm looking for a solution for some days for my problem but I can't find a good one. So I wanted to ask here again before I start developing a native app for Android since I can't solve this problem.

As the title says, I'm trying to upload an image by using webview to a html content which NORMALLY is choosing an image, showing it on the page and allowing user to crop it. I made whole app and had tested it on normal browser of my phone so I didn't know that this woulnd't work. That's why it's damn frustrating.

Those solutions which I've found on internet are not working for my Android and I read that it's nor possible anymore.

Does anyone know if this is possible or not? It would be a great help if I could get more informtions about this (even if it's possible or not?) Thanks in Adavnce...

解决方案

Try adding these permissions.

<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 has added WebChromeClient.onShowFileChooser. They even provide a way to automatically generate the file chooser intent so that it uses the input accept mime types.

Implement it in this way (from 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;
            }
        }
    }
}

Make sure to compile the app with API 21+. And this will work on all the platforms as you mention on your gradle.

这篇关于Android,无法使用 WebView 上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆