选择图库中的图像时应用程序崩溃
App crashes when image from gallery gets selected
我的应用程序中的相机功能真让我抓狂!!!
我在我的应用程序中构建了一个相机功能,但是当我尝试 select 来自图库的图像时它崩溃了。
Bitmap photo;
private static final int RESULT_LOAD_IMAGE = 1;
private static final int REQUEST_IMAGE_CAPTURE = 2;
ImageView imageView;
private void activeTakePhoto() { // if select open camera
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
private void activeGallery() { // if select choose from gallery
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver()
.query(selectedImage, filePathColumn, null, null,
null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap a = (BitmapFactory.decodeFile(picturePath));
photo = scaleBitmap(a, 200, 200);
imageView.setImageBitmap(photo);
//photo = decodeSampledBitmapFromUri(picturePath, 100, 20);
imageView.setImageBitmap(photo);
}
case REQUEST_IMAGE_CAPTURE:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
//to generate random file name
String fileName = "tempimg.jpg";
try {
photo = (Bitmap) data.getExtras().get("data");
//captured image set in imageview
imageView.setImageBitmap(photo);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果我删除 activeTakePhoto()
和 case REQUEST_IMAGE_CAPTURE:
之后的所有行,selected 图像可以显示在 imageView
上。
如果我删除activeGallery()
,捕获的图像可以在imageView
上显示。
但是,如果我使用我在此处发布的片段代码(两者都起作用),当我 select 来自画廊的图像 logCat
错误
12-10 14:49:03.507 927-927/? E/HwSystemManager﹕ :ACTION_BATTERY_CHANGED pluged =2
12-10 14:49:07.020 19428-19428/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/832821 flg=0x1 }} to activity {com.example.project.myapplication/com.example.project.myapplication.GUI.AddMoreClaims}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3579)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3622)
at android.app.ActivityThread.access00(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1436)
并且该行是 Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
。但是,当我select截取图像时,截取的图像仍然可以在imageView
上显示。
你错过了休息时间;在开关盒块之间。
一旦设置了 break,你就不会得到异常,因为 case 'REQUEST_IMAGE_CAPTURE'
不会发生失败
我的应用程序中的相机功能真让我抓狂!!!
我在我的应用程序中构建了一个相机功能,但是当我尝试 select 来自图库的图像时它崩溃了。
Bitmap photo;
private static final int RESULT_LOAD_IMAGE = 1;
private static final int REQUEST_IMAGE_CAPTURE = 2;
ImageView imageView;
private void activeTakePhoto() { // if select open camera
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
private void activeGallery() { // if select choose from gallery
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver()
.query(selectedImage, filePathColumn, null, null,
null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap a = (BitmapFactory.decodeFile(picturePath));
photo = scaleBitmap(a, 200, 200);
imageView.setImageBitmap(photo);
//photo = decodeSampledBitmapFromUri(picturePath, 100, 20);
imageView.setImageBitmap(photo);
}
case REQUEST_IMAGE_CAPTURE:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
//to generate random file name
String fileName = "tempimg.jpg";
try {
photo = (Bitmap) data.getExtras().get("data");
//captured image set in imageview
imageView.setImageBitmap(photo);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果我删除 activeTakePhoto()
和 case REQUEST_IMAGE_CAPTURE:
之后的所有行,selected 图像可以显示在 imageView
上。
如果我删除activeGallery()
,捕获的图像可以在imageView
上显示。
但是,如果我使用我在此处发布的片段代码(两者都起作用),当我 select 来自画廊的图像 logCat
错误
12-10 14:49:03.507 927-927/? E/HwSystemManager﹕ :ACTION_BATTERY_CHANGED pluged =2
12-10 14:49:07.020 19428-19428/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/832821 flg=0x1 }} to activity {com.example.project.myapplication/com.example.project.myapplication.GUI.AddMoreClaims}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3579)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3622)
at android.app.ActivityThread.access00(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1436)
并且该行是 Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
。但是,当我select截取图像时,截取的图像仍然可以在imageView
上显示。
你错过了休息时间;在开关盒块之间。
一旦设置了 break,你就不会得到异常,因为 case 'REQUEST_IMAGE_CAPTURE'