我使用另一篇文章中的以下代码来启用 HTML 表单输入(文件上传)中的文件处理。目前它与相机完美配合并从存储上传,但是如果我单击文件输入,然后不选择应用程序或单击返回,我会收到以下消息
Attempt to invoke virtual method 'android.content.ClipData android.content.Intent.getClipData()' on a null object reference
我认为这与文件选择器期望数据有关,但我返回一个空对象,因为没有图像可以通过 html 表单上传,但不知道如何修复它。我尝试了这里的方法( Cancel a file upload in a Webview )但没有运气。我使用模拟器时遇到了完整的 Android 崩溃; if (resultCode != RESULT_OK)
等我认为是正确的路径。
方法完整;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != INPUT_FILE_REQUEST_CODE || mUploadMessage == null) {
super.onActivityResult(requestCode, resultCode, data);
return;
}
try {
String file_path = mCameraPhotoPath.replace("file:","");
File file = new File(file_path);
size = file.length();
}catch (Exception e){
Log.e("Error!", "Error while opening image file" + e.getLocalizedMessage());
}
if (data != null || mCameraPhotoPath != null) {
Integer count = 1;
ClipData images = null;
try {
images = data.getClipData();
}catch (Exception e) {
Log.e("Error!", e.getLocalizedMessage());
}
if (images == null && data != null && data.getDataString() != null) {
count = data.getDataString().length();
} else if (images != null) {
count = images.getItemCount();
}
Uri[] results = new Uri[count];
// Check that the response is a good one
if (resultCode == Activity.RESULT_OK) {
if (size != 0) {
// If there is not data, then we may have taken a photo
if (mCameraPhotoPath != null) {
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
}
} else if (data.getClipData() == null) {
results = new Uri[]{Uri.parse(data.getDataString())};
} else {
for (int i = 0; i < images.getItemCount(); i++) {
results[i] = images.getItemAt(i).getUri();
}
}
}
mUploadMessage.onReceiveValue(results);
mUploadMessage = null;
}
}
我使用的是 SDK 22、工具 25.0.0、minsdk 21、目标 22。
我仍在学习 Java,因此任何解决我的问题的提示或示例将不胜感激。
谢谢
最佳答案
我的猜测是,当您调用 getClipData() 方法时,“data”对象为 NULL
关于java - Android WebView 文件处理在取消/返回时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46260275/