2016-08-19 4 views
0

フェッチされた連絡先イメージのサイズを確認し、必要に応じてサイズを変更しようとしています。私は公式の開発者アンドロイドサイトのパターンhttps://developer.android.com/training/displaying-bitmaps/load-bitmap.htmlで提案されたものを少し変更して使用しています。これは私がカーソル(それが重要ならば、私は本当に知らない)内部からdecodeSampledBitmapFromBufferedStreamメソッドを呼んでいる私のコードBufferedInputStreamから画像をサイズ変更

private 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; 
     } 
    } 

    return inSampleSize; 
} 

public static Bitmap decodeSampledBitmapFromBufferedStream(BufferedInputStream bufferedInputStream, 
                int reqWidth, int reqHeight) throws IOException 
{ 
    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 

    // Calculate inSampleSize 
    BitmapFactory.decodeStream(bufferedInputStream, new Rect(), options); 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeStream(bufferedInputStream, new Rect(), options); 
} 

です。私の問題は、オプションオブジェクトのoutHeightとoutwidthは常に0で、戻りビットマップはnullであるということです。私はそれがbufferedInoutStreamオブジェクトの再利用と関係があると思いますが、それを解決する方法はわかりません。前もって感謝します。

答えて

0

ストリームが移動したら、通常は戻ることができません(ファイルストリームのような巻き戻し方法がない限り)。ストリームを閉じて新しいストリームを開くか、最初に完全なストリームをバイト配列に読み込み、代わりにdecodeByteArrayの両方の時間を使用することができます。

+0

ありがとうございました。ちょうどそれを試して、それは働いた。 –

関連する問題