![](/img/trans.png)
[英]Jsoup HTML Parsing work on java but doesn't work on android studio
[英]Android Studio java - decodeSampledBitmapFromResource doesn't work
我是Android Studio的新手,我正在尝试制作一个包含大量图像的应用程序,当然我收到了“ OutOfMemory”错误。 我已经搜索了解决方案,并在此主题中找到了一种建议的方法来解决它https://developer.android.com/topic/performance/graphics/load-bitmap.html
我完成了相同的方法,但是在调用了decodeSampledBitmapFromResource之后,图像没有出现
这是我的代码的一部分
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.step2pointaprob4exc2img);
image.setImageBitmap(ImageNiver.decodeSampledBitmapFromResource(getResources(),R.id.step2pointaprob4exc2img,reqwidth1,reqheight1));
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int reqwidth1 = size.x;
int reqheight1 = size.y;
String s = String.valueOf(reqwidth1);
String ss = String.valueOf(reqheight1);
Log.i("value of s :",s);
Log.i("value of ss :",ss);
这是我在imageNiver类上使用的方法
public class ImageNiver {
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
// This offers some additional logic in case the image has a strange
// aspect ratio. For example, a panorama may have a much larger
// width than height. In these cases the total pixels might still
// end up being too large to fit comfortably in memory, so we should
// be more aggressive with sample down the image (=larger inSampleSize).
long totalPixels = width * height / inSampleSize;
// Anything more than 2x the requested pixels we'll sample down further
final long totalReqPixelsCap = reqWidth * reqHeight * 2;
while (totalPixels > totalReqPixelsCap) {
inSampleSize *= 2;
totalPixels /= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
}
我一直在寻找解决方案好几天了,但是找不到这个问题的答案。
在这一行:
image.setImageBitmap(ImageNiver.decodeSampledBitmapFromResource(getResources(),R.id.step2pointaprob4exc2img,reqwidth1,reqheight1));
两次查看此R.id.step2pointaprob4exc2img
您应该在资源(实际图片)中传递图片的ID,而不是像在此那样传递imageView的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.