【问题标题】:Can't set the image to imageview from image gallery in Android无法从 Android 中的图片库将图片设置为 imageview
【发布时间】:2014-06-24 18:07:59
【问题描述】:
@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 错误

  • 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
  • 在 com.leadconcept.whopopsup.CameraActivity.onActivityResult(CameraActivity.java:131)

第二个 logcat 错误将我带到这行代码:

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

该代码有什么问题?我不明白为什么我不能将 imageUri 发送到 imageview。我还验证了我传递给 setImageURI 函数的 Uri 值是正确的。

【问题讨论】:

    标签: android android-intent imageview android-gallery


    【解决方案1】:
    img_in_dailogue = (ImageView) findViewById(R.id.img_in_dailogue);
    ImageView img_in_dailogue = new ImageView(this);
    

    在初始化之前你不是在使用img_in_dailogue(顺便说一句,它是“对话”,不是对话)吗?不应该是:

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

    HTH。

    【讨论】:

    • 是的,这是一个拼写错误。我会试试这个 ImageView img_in_dailogue = new ImageView(this);
    • 如果 imageview 已经在 xml 中,为什么我们需要做 new ImageView(this)。我们不能设置唯一的 Uri 变量。 img_in_dailogue.setImageURI(Uri.parse(selectedImageUri.toString()))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多