简体   繁体   中英

Can't set the image to imageview from image gallery in Android

@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_CANCELED) {
      if (requestCode == PICK_IMAGE) {
        Uri selectedImageUri = data.getData();
        Log.d("PICK_IMAGE", selectedImageUri.toString());
        Dialog settingsDialog = new Dialog(this);
        settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.custom_dailogue, null));
        img_in_dailogue = (ImageView) findViewById(R.id.img_in_dailogue);
        ImageView img_in_dailogue = new ImageView(this);
        LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        img_in_dailogue.setLayoutParams(vp);
        img_in_dailogue.setMaxHeight(250);
        img_in_dailogue.setMaxWidth(350);
        img_in_dailogue.setImageURI(Uri.parse(selectedImageUri.toString()));
        settingsDialog.show();
      }
    }
  }

Logcat error

  • java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://media/external/images/media/232 flg=0x1 }} to activity {com.leadconcept.whopopsup/com.leadconcept.whopopsup.CameraActivity}: java.lang.NullPointerException
  • at com.leadconcept.whopopsup.CameraActivity.onActivityResult(CameraActivity.java:131)

The second logcat error takes me to this line of code:

img_in_dailogue.setImageURI(Uri.parse(selectedImageUri.toString()));

What is the problem with that code? I can't understand why I can't send imageUri to an imageview. I also verified that the Uri value I am passing to setImageURI function is correct.

img_in_dailogue = (ImageView) findViewById(R.id.img_in_dailogue);
ImageView img_in_dailogue = new ImageView(this);

Aren't you using img_in_dailogue (it's "dialogue" btw, not dailogue), before initialising it? Shouldn't it be:

ImageView img_in_dailogue = new ImageView(this);
img_in_dailogue = (ImageView) findViewById(R.id.img_in_dailogue);

HTH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM