2017-06-14 2 views
0

小さいバージョンをメモリに読み込むためにビットマップを縮小しようとしています。私は、リソースの代わりにイメージギャラリーからロードすることを除いて、Googleの例(検索は大きなビットマップを効率的に読み込む)にかなり従っています。しかし、私は次元を計算した後にヌルビットマップを取り戻しているようです。私のコードは以下の通りです:ビットマップの縮小版をロードしようとしたときにヌル

/** OnActivityResult Method **/ 
    final Uri imageUri = data.getData(); 
    final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri); 
    Bitmap bitmapToLoad = Util.decodeSampledBitmapFromResource(imageStream, 500, 500); // bitmapToLoad is null. 

    mIvScreenshot.setImageBitmap(bitmapToLoad); 


/**Helper Methods **/ 
    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; 
       } 
      } 

      return inSampleSize; 
     } 

     public static Bitmap decodeSampledBitmapFromResource(InputStream is, 
                  int reqWidth, int reqHeight) { 
      Rect rect = new Rect(); 

      // First decode with inJustDecodeBounds = true to check dimensions 
      final BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(is, rect, options); 

      // Calculate inSampleSize 
      options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

      // Decode bitmap with inSampleSize set 
      options.inJustDecodeBounds = false; 
      return BitmapFactory.decodeStream(is, rect, options); 
     } 

誰かが間違っていることをキャッチすることはできますか?

+0

あなたはURIを取得し、入力ストリームを開くことができますか?最終的なUri imageUri = data.getData(); final InputStream imageStream = getActivity()。getContentResolver()。openInputStream(imageUri);? – Juan

+1

https://stackoverflow.com/a/13872663/ 833647入力ストリームを「実際に」読む前にリセットする必要があります。 –

答えて

0

私はそれを稼働させることができました。スケーリングロジックのBiraj Zalavadia(How to reduce an Image file size before uploading to a server)とカーソルコード(How to return workable filepath?)に感謝します。ここに私のonActivityResult()があります:

try { 
     final Uri imageUri = data.getData(); 

     String[] filePath = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = getActivity().getContentResolver().query(imageUri, filePath, null, null, null); 
     cursor.moveToFirst(); 
     String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0])); 

     Uri newUri = Uri.parse(ScalingUtilities.scaleFileAndSaveToTmp(imagePath, 500, 500)); 

     final Bitmap selectedImage = BitmapFactory.decodeFile(newUri.getEncodedPath()); 
     mIvScreenshot.setImageBitmap(selectedImage); 
    } catch (Exception e) { 
     // Handle 
    } 
関連する問題