簡體   English   中英

將圖庫圖片顯示到 ImageView Android 中時出現黑屏

[英]Getting Black Screen when displaying Gallery picture into ImageView 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 和
路徑 = /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();
}

嘗試以下代替Bitmap bmImg = BitmapFactory.decodeFile(pictureFile); ?

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

ImageView具有基於 OpenGL 的顯示分辨率限制。 通常這個限制大約是2048 x 2048 如果超過該限制, ImageView將只顯示黑色並且不會拋出任何錯誤。

這對於畫廊圖像/相機圖像尤其令人惱火,因為它們的分辨率可能非常高。

這也很煩人,因為 OpenGL 限制可能因設備而異。 因此,該錯誤很難重現。

您可以使用以下內容查詢確切的最大分辨率:

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

這個答案更詳細地介紹了 OpenGL 以及這些關於位圖最大寬度和高度的限制

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM