我正在尝试将图库中的图像显示为SimpleDraweeView。我不知道这是否是这样做的方法,但我正在做的是进入图库,选择图像并获取图像路径。之后我使用galleryImage.setImageURI(Uri.parse(url));
将图像设置为SimpleDraweeView(这是galleryImage的类型)。不幸的是,我得到Bitmap too large to be uploaded into a texture (4128x2322, max=4096x4096)
。
这是我打开图库并返回imagePath的代码。
public void loadImagefromGallery(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
// When Image is selected from Gallery
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgPath = cursor.getString(columnIndex);
cursor.close();
Toast.makeText(this, imgPath,
Toast.LENGTH_LONG).show();
String url = "file://" + imgPath;
galleryImage.setImageURI(Uri.parse(url));
} 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();
}
}
编辑:
ImageRequestBuilder imageRequestBuilder = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
.setAutoRotateEnabled(true)
.setResizeOptions(new ResizeOptions(galleryImage.getLayoutParams().width, galleryImage.getLayoutParams().height));
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setImageRequest(imageRequestBuilder.build())
.setOldController(galleryImage.getController())
.build();
galleryImage.setController(controller);
答案 0 :(得分:4)
ImagePipelineConfig frescoConfig = ImagePipelineConfig.newBuilder(getApplicationContext()) .setDownsampleEnabled(true).build();
Fresco.initialize(this, frescoConfig);
这对我有用。
答案 1 :(得分:1)
您需要使用ImageRequestBuilder和Drawee Controller来设置调整大小选项,以便在放入纹理之前调整太大(宽度或高度超过2048)的图像的大小。像这样:
ImageRequestBuilder imageRequestBuilder = ImageRequestBuilder.newBuilderWithSource(Uri.parse(uri))
.setAutoRotateEnabled(true)
.setResizeOptions(new ResizeOptions(getLayoutParams().width, getLayoutParams().height));
DraweeController controller = Fresco.newDraweeControllerBuilder()
.setImageRequest(imageRequestBuilder.build())
.setOldController(getController())
.build();
setController(controller);