1
さまざまなサイズのURLからイメージをロードしています。小さなものから来ているようですが、大きなものに対してはnullPointerException(下のLog.dに表示されています)が表示されます。これらの画像のサイズを変更するにはどうすればよいですか?Android 2.2 SDK - ビットマップイメージのサイズ変更nullPointerException
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);
}
もう少し具体的にすることはできますか?あなたのNPEはどこに投げられますか?行番号?どのコード行ですか? – WarrenFaith