我正在将一张位图加载到ImageView中,但遇到了这个错误。我了解到这个限制与OpenGL硬件纹理的大小限制有关(2048x2048)。我需要加载的图片是一个大约有4000个像素高度的缩放图片。
我尝试在清单文件中关闭硬件加速,但没有成功。
<application
android:hardwareAccelerated="false"
....
>
是否可以将大小超过2048像素的图像加载到ImageView中?
我正在将一张位图加载到ImageView中,但遇到了这个错误。我了解到这个限制与OpenGL硬件纹理的大小限制有关(2048x2048)。我需要加载的图片是一个大约有4000个像素高度的缩放图片。
我尝试在清单文件中关闭硬件加速,但没有成功。
<application
android:hardwareAccelerated="false"
....
>
是否可以将大小超过2048像素的图像加载到ImageView中?
我曾经遇到同样的问题,这是我的解决方案。将图像的宽度设置为与Android屏幕宽度相同,然后按比例缩放高度。
Bitmap myBitmap = BitmapFactory.decodeFile(image.getAbsolutePath());
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Log.e("Screen width ", " "+width);
Log.e("Screen height ", " "+height);
Log.e("img width ", " "+myBitmap.getWidth());
Log.e("img height ", " "+myBitmap.getHeight());
float scaleHt =(float) width/myBitmap.getWidth();
Log.e("Scaled percent ", " "+scaleHt);
Bitmap scaled = Bitmap.createScaledBitmap(myBitmap, width, (int)(myBitmap.getWidth()*scaleHt), true);
myImage.setImageBitmap(scaled);
Glide.with(this).load(Uri.parse(filelocation))).into(img_selectPassportPic);
缩小图像:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// Set height and width in options, does not return an image and no resource taken
BitmapFactory.decodeStream(imagefile, null, options);
int pow = 0;
while (options.outHeight >> pow > reqHeight || options.outWidth >> pow > reqWidth)
pow += 1;
options.inSampleSize = 1 << pow;
options.inJustDecodeBounds = false;
image = BitmapFactory.decodeStream(imagefile, null, options);
图像将按照reqHeight和reqWidth的大小进行缩小。据我所知,inSampleSize只接受2的幂值。
我尝试了上面所有的解决方案,一个接一个地尝试了好几个小时,但似乎都不起作用!最后,我决定寻找一些关于使用Android相机捕获图像并显示的官方示例。官方示例(在这里)终于给了我唯一有效的方法。下面是我在该示例应用程序中找到的解决方案:
public void setThumbnailImageAndSave(final ImageView imgView, File imgFile) {
/* There isn't enough memory to open up more than a couple camera photos */
/* So pre-scale the target bitmap into which the file is decoded */
/* Get the size of the ImageView */
int targetW = imgView.getWidth();
int targetH = imgView.getHeight();
/* Get the size of the image */
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imgFile.getAbsolutePath(), bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
/* Figure out which way needs to be reduced less */
int scaleFactor = 1;
if ((targetW > 0) || (targetH > 0)) {
scaleFactor = Math.min(photoW/targetW, photoH/targetH);
}
/* Set bitmap options to scale the image decode target */
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
/* Decode the JPEG file into a Bitmap */
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), bmOptions);
/* Associate the Bitmap to the ImageView */
imgView.setImageBitmap(bitmap);
imgView.setVisibility(View.VISIBLE);
}
使用正确的drawable子文件夹解决了我的问题。我的解决方案是将全分辨率图像(1920x1200)放入drawable-xhdpi文件夹中,而不是drawable文件夹。
我还将缩小的图像(1280x800)放入drawable-hdpi文件夹中。
这两个分辨率与我正在编程的2013年和2012年Nexus 7平板电脑相匹配。我还在其他平板电脑上测试了解决方案。
想要添加小尺寸图片的注意事项:
Pilot_51的解决方案(将您的图像移动到drawable-nodpi
文件夹中)可以解决问题,但还有另一个问题:除非将图像调整为非常大的分辨率(如2000 x 3800)以适应屏幕,否则它会使图像在屏幕上过于小——然后会使您的应用程序更重。
解决方案:将您的图像文件放在drawable-hdpi
中——这对我来说起作用了。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
///*
if (requestCode == PICK_FROM_FILE && resultCode == RESULT_OK && null != data){
uri = data.getData();
String[] prjection ={MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri,prjection,null,null,null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(prjection[0]);
ImagePath = cursor.getString(columnIndex);
cursor.close();
FixBitmap = BitmapFactory.decodeFile(ImagePath);
ShowSelectedImage = (ImageView)findViewById(R.id.imageView);
// FixBitmap = new BitmapDrawable(ImagePath);
int nh = (int) ( FixBitmap.getHeight() * (512.0 / FixBitmap.getWidth()) );
FixBitmap = Bitmap.createScaledBitmap(FixBitmap, 512, nh, true);
// ShowSelectedImage.setImageBitmap(BitmapFactory.decodeFile(ImagePath));
ShowSelectedImage.setImageBitmap(FixBitmap);
}
}
这段代码是可行的
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:hardwareAccelerated="false"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />