将Gallery图片显示到ImageView Android时获取黑屏

时间:2017-05-12 08:39:51

标签: android

 @Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_read_palm:
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
            break;
        case R.id.btn_read_from_existing:
            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, RESULT_LOAD_IMG);
            break;
    }
}
  

此处获取selectedImageUri = content:// media / external / images / media / 2997         和
  path = /storage/emulated/0/DCIM/Camera/IMG_20170510_132342860.jpg

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {
            case CAMERA_REQUEST:
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                saveAndShowPictureDialog(photo);
                break;
            case RESULT_LOAD_IMG:
                Uri selectedImageUri = data.getData();
                String path = getRealPathFromURI(this, selectedImageUri);
                if (path != null)
                    showImageDialog(path);
        }
    }
}
  

getRealPathFromURI函数返回正确的路径。

 public String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Images.Media.DATA};
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        assert cursor != null;
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}


private void showImageDialog(String pictureFile) {
    // custom dialog
    final Dialog dialog = new Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    dialog.setContentView(R.layout.image_dialog);
    dialog.setTitle("Image");
    // find the imageview and draw it!
    ImageView image = (ImageView) dialog.findViewById(R.id.image);
    Bitmap bmImg = BitmapFactory.decodeFile(pictureFile);
  

在图片文件中设置图像位图时,我在imageview上获得黑屏/图像。

    image.setImageBitmap(bmImg);
    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    dialog.show();
}

2 个答案:

答案 0 :(得分:0)

请尝试以下操作而不是Bitmap bmImg = BitmapFactory.decodeFile(pictureFile);

            Bitmap bmImg;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            bmImg = BitmapFactory.decodeFile(selectedImagePath, options);
            image.setImageBitmap(bmImg);

答案 1 :(得分:0)

ImageView具有基于OpenGL的显示分辨率限制。通常,此限制约为2048 x 2048。如果您超过该限制,则ImageView只会显示黑色,并且不会出错。

这对于图库图像/相机图像特别令人讨厌,因为它们可能具有很高的分辨率。

这也很令人讨厌,因为OpenGL的限制可能会因设备而异。因此,该错误很难重现。

您可以使用以下内容查询确切的最大分辨率:

int[] maxSize = new int[1];
GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxSize, 0);

This answer进一步详细介绍了OpenGL及其与位图最大宽度和高度有关的限制