简体   繁体   English

我得到黑屏而不是照片

[英]I get black screen instead of photo

I got problem with my image loading in my Fragment, when i chose place to search for image and pick the picture instead of it i get the black circle, this is my code: 我在Fragment中加载图片时遇到问题,当我选择在哪里搜索图片并选择图片而不是图片时,出现黑圈,这是我的代码:

civ = view.findViewById(R.id.circle_profile);

civ.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(GaleryIntent, RESULT_LOAD_IMAGE);
    }
});

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == getActivity().RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getActivity().getContentResolver().query(selectedImage,filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        CircleImageView imageView =  getActivity().findViewById(R.id.circle_profile);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}

XML: XML:

<de.hdodenhof.circleimageview.CircleImageView
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_column="0"
    android:layout_row="1"
    android:layout_rowSpan="3"
    android:layout_marginLeft="30dp"
    android:layout_columnWeight="1"
    android:id="@+id/circle_profile"
    android:src="@drawable/profile"
    app:civ_border_color="#FFFFFF"
    app:civ_border_width="2dp"
    />

It's looks like the border color only left 好像只剩下边框颜色

I suspect that in your code: BitmapFactory.decodeFile(picturePath) returns null maybe because the size of image is large 我怀疑在您的代码中: BitmapFactory.decodeFile(picturePath)返回null可能是因为图像的大小很大

I suggest using BitmapFactory.Options and pass it inside decodeFile function with BitmapFactory.Options as second parameter. 我建议使用BitmapFactory.Options并将其传递给带有BitmapFactory.Options作为第二个参数的decodeFile功能。

EDIT: 编辑:

BitmapFactory.Options can have code like below: BitmapFactory.Options可以具有如下代码:

// Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;


   BitmapFactory.decodeFile(picturePath, bmOptions);

int photoW= bmOptions.outWidth;
int photoH = bmOptions.outHeight;

// Get the dimensions of the View
    int targetW = imageView.getWidth();
    int targetH = imageView.getHeight();

//Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

//Decode the image file into a Bitmap sized to fill the view
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;

Bitmap bitmap = BitmapFactory.decodeFile(picturePath, bmOptions);
imageView.setImageBitmap(bitmap);

Hope it helps! 希望能帮助到你!

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

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