Hai我正在开发一个应用程序,用户必须通过从图库中选择图像或从相机拍摄新图像来上传图像。当我从图库中选择图像时测试应用程序工作正常。但是当我使用相机捕获新图像应用程序崩溃异常 由以下原因引起:java.lang.nullpointerexception:尝试调用虚拟方法' android.net.uri android.content.intent.getdata()'在空对象引用上
声明意图:
final CharSequence[] dilogitems = {"Take Photo","Select from gallery","cancel"};
final AlertDialog.Builder diloguebuilder = new AlertDialog.Builder(addlog.this);
diloguebuilder.setTitle("Add Photo");
diloguebuilder.setItems(dilogitems, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
if (dilogitems[i].equals("Take Photo"))
{
Intent camIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (camIntent.resolveActivity(getPackageManager())!=null)
{
File file = new File(Environment.getExternalStorageDirectory()+File.separator+"image.jpg");
camIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(file));
startActivityForResult(camIntent,TAKE_PICTURE);
}
}
else if (dilogitems[i].equals("Select from gallery"))
{
Intent pickgallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickgallery.setType("image/*");
if (pickgallery.resolveActivity(getPackageManager())!=null)
{
startActivityForResult(Intent.createChooser(pickgallery,"Pick an Application"),SELECT_FROM_GALLERY);
}
}
else if (dilogitems[i].equals("cancel"))
{
dialogInterface.dismiss();
}
}
});
diloguebuilder.show();
关于活动结果
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==TAKE_PICTURE&&requestCode == Activity.RESULT_OK)
{
try {
File tempimage = new File(Environment.getExternalStorageDirectory()+File.separator+"image.jpg");
personImg.setImageBitmap(decodefile(tempimage.getAbsolutePath()));
} catch (Exception e) {
Toast.makeText(addlog.this, "Error Loading Image", Toast.LENGTH_SHORT).show();
}
}
else if(requestCode== SELECT_FROM_GALLERY&& resultCode == Activity.RESULT_OK)
{
image = data.getData();
try
{
persinbitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),image);
personImg.setImageBitmap(persinbitmap);
personImg.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
catch (Exception e)
{
Toast.makeText(addlog.this, "Error loading Image", Toast.LENGTH_SHORT).show();
}
}
}
decodeFile()方法:
private Bitmap decodefile(String absolutePath)
{
final BitmapFactory.Options options = new BitmapFactory.Options();
int reqheight = persinbitmap.getHeight();
int reqwidth = persinbitmap.getWidth();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(absolutePath,options);
int height = options.outHeight;
int width = options.outWidth;
int inSampleSize = 1;
if (height > reqheight) {
inSampleSize = Math.round((float)height / (float)reqheight);
}
int expectedWidth = width / inSampleSize;
if (expectedWidth > reqwidth) {
//if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
inSampleSize = Math.round((float)width / (float)reqwidth);
}
options.inSampleSize = inSampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(absolutePath, options);
}
这是Log cat:
java.lang.RuntimeException: Unable to resume activity {com.example.mrudu.accounts/com.example.mrudu.accounts.addlog}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.mrudu.accounts/com.example.mrudu.accounts.addlog}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2995)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3030)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.mrudu.accounts/com.example.mrudu.accounts.addlog}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3626)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2969)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3030)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference
at com.example.mrudu.accounts.addlog.onActivityResult(addlog.java:104)
at android.app.Activity.dispatchActivityResult(Activity.java:6161)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3622)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2969)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3030)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
提前致谢...