gpt4 book ai didi

Android,无法使用WebView上传图片

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:36:55 24 4
gpt4 key购买 nike

几天来我一直在为我的问题寻找解决方案,但找不到好的解决方案。所以我想在开始为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com