【发布时间】:2017-09-17 03:08:31
【问题描述】:
我正在使用另一篇文章中的以下代码来启用 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,因此任何能解决我的问题的提示或示例将不胜感激。
谢谢
【问题讨论】: