gpt4 book ai didi

Android 2.2 SDK - 位图图像调整大小 nullPointerException

转载 作者:行者123 更新时间:2023-11-29 18:22:04 25 4
gpt4 key购买 nike

我正在从不同大小的 URLS 加载图像。似乎较小的出现了,而较大的出现了 nullPointerException(显示在下面的 Log.d 中)。如何调整这些图片的大小?

                BitmapDrawable drawable = null;
Bitmap bitmap = null;
try {
bitmap = loadBitmapFromWeb(url);
System.out.println("url " + url);
} catch (IOException e) {
}

int width = 150;
int height = 150;
try {
drawable = resizeImage(bitmap, height, width);
} catch (Exception e) {
Log.d("exception image", e.toString());
drawable = getDrawableFromResource(R.drawable.default_backup);
}

这是我用来从 URL 加载的内容:

    public static Bitmap loadBitmapFromWeb(String url) throws IOException {
InputStream is = (InputStream) new URL(url).getContent();
Bitmap bitmap = BitmapFactory.decodeStream(is);

return bitmap;
}

这是我用来调整大小,出现错误的地方:

    public static BitmapDrawable resizeImage(Bitmap bitmap, int w, int h) {

int width = bitmap.getWidth();
int height = bitmap.getHeight();
int newWidth = w;
int newHeight = h;

float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;

Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);

Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,
matrix, true);

return new BitmapDrawable(resizedBitmap);
}

最佳答案

为什么不使用可用的位图函数来调整大小。假设您下载了正确的图像,您只需执行以下操作:

    Bitmap scaledImage = Bitmap.createScaledBitmap(originalImage, newWidth, newHeight, false);

关于Android 2.2 SDK - 位图图像调整大小 nullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4857728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com