2012-07-31 93 views
1

我試圖調整從SD卡中選擇一個縮略圖大小的圖片,但是當我調整它的大小,在尺寸上我的默認圖像128/128位圖是空位圖圖像爲null調整後

InputStream input=null; 
      try { 
       input = getActivity().getContentResolver().openInputStream(Uri.parse(cursor.getString(3))); 
       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inJustDecodeBounds = true; 
       BitmapFactory.decodeStream(input, null, options); 
       int height = options.outHeight; //1030 
       int width = options.outWidth; //1033 
       BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture, options); 

       int imageHeight = options.outHeight; //128 
       int imageWidth = options.outWidth; //128 


       if(imageWidth > imageHeight){ 
        options.inSampleSize = Math.round((float)height/(float)imageHeight); 
       }else{ 
        options.inSampleSize = Math.round((float)width/(float)imageWidth); 
       } 
       options.inJustDecodeBounds = false; 
       Bitmap bm = BitmapFactory.decodeStream(input,null, options); //null here 
       try { 
        input.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       image.setImageBitmap(bm); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 

我可以同時獲得圖像的高度和寬度,但是當我去調整它的大小時,它會返回一個空位圖。

我已經這樣用的資源文件,但從來沒有從SD卡等什麼我做錯了圖像的Uri做呢?

回答

0

不能使用相同的輸入流的兩倍,因此您需要創建新的InputStream第二次。有關更多信息,請參閱here

+0

啊不知道的是,這是原因 – tyczj 2012-07-31 00:36:00