[英]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.