我正在点击图像并在ImageView中显示它。下面是我的代码。下面的代码在授予权限时执行。
private void clickImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageFile = new File(itemName.getText().toString()+".jpg");
Uri tempuri = Uri.fromFile(imageFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT,tempuri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
startActivityForResult(intent,0 );
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 0)
{
switch (resultCode){
case Activity.RESULT_OK:
Log.d("path",imageFile.getAbsolutePath());
Toast.makeText(this,"file saved"+imageFile.getAbsolutePath(),Toast.LENGTH_LONG).show();
ImageView imv = (ImageView) findViewById(R.id.itemImage);
Bitmap photo = (Bitmap) data.getExtras().get("data");
imv.setImageBitmap(photo);
break;
case Activity.RESULT_CANCELED:
break;
default:break;
}
}
}
错误日志。
03-01 17:51:47.183 5883-5883/com.lab.yourhomebasket E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lab.yourhomebasket, PID: 5883
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.lab.yourhomebasket/com.lab.yourhomebasket.AddItemActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3720)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3763)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at com.lab.yourhomebasket.AddItemActivity.onActivityResult(AddItemActivity.java:107)
at android.app.Activity.dispatchActivityResult(Activity.java:6470)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3716)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3763)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
我正在使用moto g3进行测试。图像未显示在我的ImageView中。请帮忙。
答案 0 :(得分:1)
引用the documentation for ACTION_IMAGE_CAPTURE
:
调用者可以传递额外的EXTRA_OUTPUT来控制该图像的写入位置。如果EXTRA_OUTPUT不存在,则在额外字段中返回小尺寸图像作为Bitmap对象。这对于只需要小图像的应用程序非常有用。如果存在EXTRA_OUTPUT,则将将全尺寸图像写入EXTRA_OUTPUT的Uri值。
您正在使用EXTRA_OUTPUT
。因此,data.getExtras().get("data")
将不起作用。如果您使用data
,则无需向您发送Intent
EXTRA_OUTPUT
。
相反,应将图像写入您提供给EXTRA_OUTPUT
的位置。