[英]App crashes when 'not' selecting an image from file choose / gallery
当我尝试选择图像时,文件选择器会显示“相机”和“文件”,如果我不选择图像并单击鼠标,它会崩溃吗? 我已经尝试了一些答案,但是我是一个webdev而不是android dev。 救命!
@Override
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;
}
}
在进行计算之前,您尚未在onActivityResult
进行任何面向resultCode
的检查。 当您不选择图像而返回时,resultCode为RESULT_CANCELLED
而您的data
为null。 发布您对data
任何操作将始终导致空指针异常
如下-
if (resultCode==RESULT_OK){
// do stuff here that is the part of your try catch block
}
多数情况下,当您尝试对不存在的数据执行data.getDataString()时,代码将崩溃。 并且考虑到这在您的try块之外,它永远不会被捕获并且应用程序崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.