-4
我點擊Image並在ImageView中顯示它。下面是我的代碼。下面的代碼在授予權限時執行。單擊圖像並在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中。請幫忙。