【发布时间】:2014-05-03 05:46:02
【问题描述】:
我需要以画廊形式全屏显示原始图像。对于拇指来说,它将完美地工作,当我尝试使用原始源全屏显示该图像时,它将无法显示。在大多数情况下,如果图像分辨率大于 2000,则会显示错误bitmap too large to be uploading into a texture android。
我想防止这种情况发生,我搜索了谷歌但没有得到任何答案。
【问题讨论】:
我需要以画廊形式全屏显示原始图像。对于拇指来说,它将完美地工作,当我尝试使用原始源全屏显示该图像时,它将无法显示。在大多数情况下,如果图像分辨率大于 2000,则会显示错误bitmap too large to be uploading into a texture android。
我想防止这种情况发生,我搜索了谷歌但没有得到任何答案。
【问题讨论】:
我遇到了同样的问题,并为这个问题想出了一个单一的解决方案here:
Picasso.with(context).load(new File(path/to/File)).fit().centerCrop().into(imageView);
【讨论】:
fit() 和 resize() 不会阻止您加载太大的图像。他们只是为您调整图像大小。
我刚刚创建了一个 if else 函数来检查图像是否大于 1M 像素,示例代码如下:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
int height = bitmap.getHeight(), width = bitmap.getWidth();
if (height > 1280 && width > 960){
Bitmap imgbitmap = BitmapFactory.decodeFile(selectedImagePath, options);
imageView.setImageBitmap(imgbitmap);
System.out.println("Need to resize");
}else {
imageView.setImageBitmap(bitmap);
System.out.println("WORKS");
}
【讨论】:
Google 提供了如何执行此操作的培训。从Displaying Bitmaps Efficiently下载示例
看看 ImageResizer 类。 ImageResizer.decodeSampledBitmapFrom* 使用此方法获取缩小的图像。
【讨论】:
这是我用来纠正在分辨率为 4096x4096 的图像视图中拟合分辨率为 3120x4196 的图像的问题的代码。这里 ImageViewId 是主布局中创建的图像视图的 id,ImageFileLocation 是要调整大小的图像的路径。
ImageView imageView=(ImageView)findViewById(R.id.ImageViewId);
Bitmap d=BitmapFactory.decodeFile(ImageFileLcation);
int newHeight = (int) ( d.getHeight() * (512.0 / d.getWidth()) );
Bitmap putImage = Bitmap.createScaledBitmap(d, 512, newHeight, true);
imageView.setImageBitmap(putImage);
【讨论】:
您不需要加载整个图像,因为它太大了,您的手机可能无法显示完整的位图像素。 您需要先根据您的设备屏幕尺寸对其进行缩放。 这是我发现的最好的方法,而且效果很好: Android: Resize a large bitmap file to scaled output file
【讨论】:
我找到了一种方法不使用任何外部库:
if (bitmap.getHeight() > GL10.GL_MAX_TEXTURE_SIZE) {
// this is the case when the bitmap fails to load
float aspect_ratio = ((float)bitmap.getHeight())/((float)bitmap.getWidth());
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0,
(int) ((GL10.GL_MAX_TEXTURE_SIZE*0.9)*aspect_ratio),
(int) (GL10.GL_MAX_TEXTURE_SIZE*0.9));
imageView.setImageBitmap(scaledBitmap);
}
else{
// for bitmaps with dimensions that lie within the limits, load the image normally
if (Build.VERSION.SDK_INT >= 16) {
BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
imageView.setBackground(ob);
} else {
imageView.setImageBitmap(bitmap);
}
}
基本上,最大图像尺寸是系统强加的限制。上述方法将正确调整超出此限制的位图大小。但是,只会加载整个图像的一部分。要更改显示的区域,您可以更改createBitmap() 方法的x 和y 参数。
这种方法可以处理任何大小的位图,包括用专业相机拍摄的照片。
参考资料:
【讨论】: