【问题标题】:ImageView in the AlertDialog is blank after picking the image from gallery and displaying it从图库中选择图像并显示后,AlertDialog 中的 ImageView 为空白
【发布时间】:2016-05-30 09:19:48
【问题描述】:

[这是显示空白ImageView 的对话框我试图在我的自定义对话框中将所选图像从图库显示到ImageView,但ImageView 显示为空白。它不是空的。下面附上代码sn-p:

  public void loadImagefromGallery(View view) {
       Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, SELECT_PHOTO);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();


                final Dialog dialog = new Dialog(SelectImagesActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.upload_image);
                ImageView imageView = (ImageView) dialog.findViewById(R.id.imageView1);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
                EditText editText = (EditText)dialog.findViewById(R.id.keyWords);
                Button button = (Button)dialog.findViewById(R.id.done);
                dialog.setCancelable(false);
                dialog.getWindow().setLayout(550, 900);
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.show();
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            } else {
            Toast.makeText(this, "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }

}

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 分享截图。
  • String picturePath = cursor.getString(columnIndex); 是否返回图像的正确绝对路径。

标签: android android-intent imageview android-dialog


【解决方案1】:

改变

 dialog.show();
 imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
dialog.show();

【讨论】:

    【解决方案2】:

    改为:

    imageView.setImageURI(Uri.parse(picturePath));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      相关资源
      最近更新 更多