2011-02-01 6 views
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); 
    } 
+0

もう少し具体的にすることはできますか?あなたのNPEはどこに投げられますか?行番号?どのコード行ですか? – WarrenFaith

答えて

3

あなたがしない理由使用可能なビットマップ関数を使用してサイズ変更を行います。正しいイメージをダウンロードしたと仮定すると、次のようにするだけです:

Bitmap scaledImage = Bitmap.createScaledBitmap(originalImage, newWidth, newHeight, false); 
関連する問題