我的申请有问题。
当我按下相机中的后退按钮而没有捕获图像时,它崩溃了。
我不明白是什么问题。
你们中的任何一个都请给我解决方案。
这是按下按钮时的代码:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("From where to select Image?")
.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GlobalVariables.window="end";
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
try {
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
} catch (ActivityNotFoundException ane) {
ane.printStackTrace();
}
}
})
.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
GlobalVariables.window="end";
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(RespAdap.path.toString()+"/Attachment.png")));
startActivityForResult(intent, 1337);
}
});
AlertDialog dialog = builder.create();
dialog.show();
这是onActivityResult的代码:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECT_PICTURE) {
if(resultCode != 0)
{
if (bitmap != null) {
bitmap.recycle(); // If bitmap is not null then here it will be recycled and set to null
bitmap = null;
}
try{
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
File fileObject = new File(selectedImagePath);
bitmap = decodeFile(fileObject);
img_selected.setImageBitmap(bitmap);
String val = getBase64Value(bitmap);
myBitmapImage=bitmap;
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
fileExt = filePath.substring(filePath.length()-3);
}
cursor.close();
img_selected.setVisibility(View.VISIBLE);
}
catch (Exception e) {
// TODO: handle exception
}
//new uploadImage().execute(val);
}
}
else if (requestCode == 1337) {
if (data == null&&resultCode==-1) {
try{File fileObject = new File(RespAdap.path.toString()+"/Attachment.png");
bitmap = decodeFile(fileObject);
//img_view.setImageBitmap(bitmap);
String val = getBase64Value(bitmap);
Matrix matrix = new Matrix();
matrix.postRotate(getCameraPhotoOrientation(getActivity(),Uri.fromFile(new File(RespAdap.path.toString()+"/Attachment.png")), RespAdap.path.toString()+"/Attachment.png"));
bitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(),
matrix, true);
myBitmapImage=bitmap;
img_selected.setVisibility(View.VISIBLE);
img_selected.setImageBitmap(bitmap);
}
catch (Exception e) {
// TODO: handle exception
}
} else {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
int imageHeight = thumbnail.getHeight();
int imageWidth = thumbnail.getWidth();
Log.v("Log_tag", "Final image Height" + imageHeight);
Log.v("Log_tag", "Final image Width" + imageWidth);
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
fileExt = filePath.substring(filePath.length()-3);
}
cursor.close();
String selectedImagePath = getPath(selectedImage);
File fileObject = new File(selectedImagePath);
bitmap = decodeFile(fileObject);
//img_view.setImageBitmap(bitmap);
String val = getBase64Value(bitmap);
Matrix matrix = new Matrix();
matrix.postRotate(getCameraPhotoOrientation(getActivity(), selectedImage, selectedImagePath));
bitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(),
matrix, true);
myBitmapImage=bitmap;
img_selected.setVisibility(View.VISIBLE);
img_selected.setImageBitmap(bitmap);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
我正在Galaxy Note 10.1上进行测试。这是为此的logcat。
09-11 20:48:24.896: E/AndroidRuntime(30747): FATAL EXCEPTION: main
09-11 20:48:24.896: E/AndroidRuntime(30747): Process: com.first.ogoing, PID: 30747
09-11 20:48:24.896: E/AndroidRuntime(30747): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=329017, result=0, data=null} to activity {com.first.ogoing/com.first.try_flymenu.ReponsiveList}: java.lang.NullPointerException
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.app.ActivityThread.deliverResults(ActivityThread.java:3574)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.app.ActivityThread.access$1400(ActivityThread.java:169)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1325)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.os.Handler.dispatchMessage(Handler.java:102)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.os.Looper.loop(Looper.java:136)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.app.ActivityThread.main(ActivityThread.java:5476)
09-11 20:48:24.896: E/AndroidRuntime(30747): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 20:48:24.896: E/AndroidRuntime(30747): at java.lang.reflect.Method.invoke(Method.java:515)
09-11 20:48:24.896: E/AndroidRuntime(30747): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
09-11 20:48:24.896: E/AndroidRuntime(30747): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
09-11 20:48:24.896: E/AndroidRuntime(30747): at dalvik.system.NativeStart.main(Native Method)
09-11 20:48:24.896: E/AndroidRuntime(30747): Caused by: java.lang.NullPointerException
09-11 20:48:24.896: E/AndroidRuntime(30747): at com.first.try_flymenu.adapter.RespChangImage.onActivityResult(RespChangImage.java:258)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:161)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.app.Activity.dispatchActivityResult(Activity.java:5665)
09-11 20:48:24.896: E/AndroidRuntime(30747): at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
09-11 20:48:24.896: E/AndroidRuntime(30747): ... 11 more
最佳答案
您的接收活动中有一个nullpointerexception:
在com.first.try_flymenu.adapter.RespChangImage.onActivityResult(RespChangImage.java:258)
在第258行,您似乎希望返回不存在的数据。
您需要在处理数据之前,在onActivityResult()
中检入if(resultcode == RESULT_OK)
。
关于android - 向后按相机时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25791288/