Fresco - 位图太大而无法上传到纹理中
Fresco - Bitmap too large to be uploaded into a texture
我正在尝试将图库中的图像显示到 SimpleDraweeView 中。我不知道这是不是这样做的方法,但我正在做的是进入画廊,选择图像并获取图像路径。之后,我使用 galleryImage.setImageURI(Uri.parse(url));
将图像设置到 SimpleDraweeView(这是 galleryImage 的类型)中。不幸的是我得到 Bitmap too large to be uploaded into a texture (4128x2322, max=4096x4096)
.
这是我打开图库和 returns 图像路径的代码。
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);
您将需要使用 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);
ImagePipelineConfig frescoConfig = ImagePipelineConfig.newBuilder(getApplicationContext()) .setDownsampleEnabled(true).build();
Fresco.initialize(this, frescoConfig);
对我有用。
我正在尝试将图库中的图像显示到 SimpleDraweeView 中。我不知道这是不是这样做的方法,但我正在做的是进入画廊,选择图像并获取图像路径。之后,我使用 galleryImage.setImageURI(Uri.parse(url));
将图像设置到 SimpleDraweeView(这是 galleryImage 的类型)中。不幸的是我得到 Bitmap too large to be uploaded into a texture (4128x2322, max=4096x4096)
.
这是我打开图库和 returns 图像路径的代码。
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);
您将需要使用 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);
ImagePipelineConfig frescoConfig = ImagePipelineConfig.newBuilder(getApplicationContext()) .setDownsampleEnabled(true).build();
Fresco.initialize(this, frescoConfig);
对我有用。