简体   繁体   English

无法将图像从Android中的图片库设置为imageview

[英]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 Logcat错误

  • 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 java.lang.RuntimeException:无法将结果ResultInfo {who = null,request = 0,result = -1,data = Intent {dat = content:// media / external / images / media / 232 flg = 0x1}}传递给活动失败{com.leadconcept.whopopsup / com.leadconcept.whopopsup.CameraActivity}:java.lang.NullPointerException
  • at com.leadconcept.whopopsup.CameraActivity.onActivityResult(CameraActivity.java:131) 在com.leadconcept.whopopsup.CameraActivity.onActivityResult(CameraActivity.java:131)

The second logcat error takes me to this line of code: 第二个logcat错误将我带到以下代码行:

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. 我不明白为什么无法将imageUri发送到imageview。 I also verified that the Uri value I am passing to setImageURI function is correct. 我还验证了我传递给setImageURI函数的Uri值正确。

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? 在初始化之前,您不是使用img_in_dailogue (它是“对话” btw,而不是dailogue)吗? Shouldn't it be: 不应该是:

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

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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