2012-09-28 68 views
3

我使用這種被廣泛知道代碼安卓BitmapFactory總是返回0

Display display = this.getWindowManager().getDefaultDisplay(); 
      float dw = display.getWidth(); 
      float dh = display.getHeight(); 

      // load image dimensions, not the image itself 
      BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
      bmpFactoryOptions.inJustDecodeBounds = true; 
      Bitmap bmp = BitmapFactory.decodeFile(MyApp.getImageFilePath()); 


      int heightRatio = (int) FloatMath.ceil(bmpFactoryOptions.outHeight/dh); 
      int widthRatio = (int) FloatMath.ceil(bmpFactoryOptions.outWidth/dw); 

      if ((heightRatio > 1) && (widthRatio > 1))// if true one side of the image is bigger than the screen 
      { 
       if (heightRatio > widthRatio) { 
        bmpFactoryOptions.inSampleSize = heightRatio; 
       } else { 
        bmpFactoryOptions.inSampleSize = widthRatio; 
       } 
      } 
      // decode it for real 
      bmpFactoryOptions.inSampleSize = bmpFactoryOptions.inSampleSize; 
      bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888; //http://www.curious-creature.org/2010/12/08/bitmap-quality-banding-and-dithering/ 
      bmpFactoryOptions.inJustDecodeBounds = false; 
      bmpFactoryOptions.inDither = true; 
      bmp = BitmapFactory.decodeFile(MyApp.getImageFilePath(), bmpFactoryOptions); 

      ImageView photo = (ImageView) this.findViewById(R.id.imageView1); 

問題是

bmpFactoryOptions.outWidth 
bmpFactoryOptions.outHeight 
bmpFactoryOptions.inSampleSize 

始終具有價值0。我已經在三種不同的設備上測試過了,我做錯了什麼?

和設置 bmpFactoryOptions.inSampleSize = bmpFactoryOptions.inSampleSize + 1;

沒有效果

+0

這是什麼'MyApp'? – codeKiller

回答

3

相反的是標題說,你BitmapFactory返回一個位圖就好了。

您從未在此聲明中分配過您的bmpFactoryOptions句柄。所以查詢它應該不會產生任何結果。增加它並沒有幫助,因爲您從未將其初始化爲圖像的屬性。

Bitmap bmp = BitmapFactory.decodeFile(MyApp.getImageFilePath()); 

做這樣的事情,而不是:

Bitmap bmp = BitmapFactory.decodeFile(MyApp.getImageFilePath(), bmpFactoryOptions); 

這將附上您的bmpFactoryOptions您BMP。然後你可以查詢選項。請注意,你的BitmapFactory返回一個完美的位圖,只是一個你沒有任何處理其屬性的位圖。

+0

非常感謝,它一直在竊聽我好幾天,我用這個Bitmap bmp = BitmapFactory.decodeFile(MyApp.getImageFilePath(),bmpFactoryOptions); – max4ever