Android 我得到的是黑屏而不是照片

Android 我得到的是黑屏而不是照片,android,image,android-intent,gallery,circleimage,Android,Image,Android Intent,Gallery,Circleimage,我在片段中加载图像时遇到问题,当我选择搜索图像的位置并选择图片时,我得到了黑色圆圈,这是我的代码: civ = view.findViewById(R.id.circle_profile); civ.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent GaleryIntent = new Intent(Intent.AC

我在片段中加载图像时遇到问题,当我选择搜索图像的位置并选择图片时,我得到了黑色圆圈,这是我的代码:

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:



看起来边框颜色只剩下了

我怀疑在您的代码中:
BitmapFactory.decodeFile(picturePath)
返回
null
可能是因为图像太大了

我建议使用
BitmapFactory.Options
并将其作为第二个参数传递到函数内部

编辑:

BitmapFactory。选项可以有如下代码:

// 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);

希望有帮助

我怀疑在您的代码中:
BitmapFactory.decodeFile(picturePath)
返回
null
可能是因为图像太大了

我建议使用
BitmapFactory.Options
并将其作为第二个参数传递到函数内部

编辑:

BitmapFactory。选项可以有如下代码:

// 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);

希望有帮助

你检查过picturePath值吗?对我来说,它看起来很好。你可以使用图像选择器库。例如,没有更好的解决方案,然后使用库。如果你尝试使用像Glide这样的图像加载库,ImageLoader加载本地图像?你检查过picturePath值吗?对我来说,它看起来不错。你可以使用图像选择器库。例如,没有更好的解决方案,然后使用库。如果你尝试使用像Glide这样的图像加载库,ImageLoader加载本地图像?我应该给出什么选项?我已经用
BitmapFactory编辑了我的答案。选项
codepicturePath我应该这样写吗?“@drawable/profile”否。它应该是字符串(就像你所说的那样)。请在这里检查语法:Thaks很多对我有帮助的东西。我应该给出什么作为选项?我已经用
BitmapFactory编辑了我的答案。选项
codepicturePath我应该这样写吗?”“@drawable/profile”否。它应该是字符串(就像你有疑问一样)。请在这里通读语法:Thaks对我帮助很大