2013-08-29 147 views
1

我想解碼一個位圖,但該函數返回null,我不知道爲什麼。解碼位圖返回null

代碼:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

if(requestCode == SELECT_FOTO) 
{ 
    Uri imgselect = data.getData(); 
    String imgpath = imgselect.getPath(); 
    File f = new File (imgpath); 
    Bitmap bm = BitmapFactory.decodeFile(f.getAbsolutePath()); 
    Toast.makeText(Insertarlugar.this, "Bitmap es" + bm, Toast.LENGTH_LONG).show(); 
} 

的敬酒指示我,BM爲空。我爲f.getPath()更改了f.getAbsolutePath(),但結果相同。 Uri imgselect和String imgpath有值。我不使用SD卡,並從圖庫中獲取位圖。

如何調整位圖大小?

謝謝。

+1

首先檢查imgpath的值 –

+0

正如我在我的帖子中所說,我檢查了imgselect的值。是內容/媒體/外部/圖像/媒體/ 11295,所以它有價值。 – axmug

回答

3

試試這一個,檢查前如果圖像有大的,所以你試試這個,讓寬度和高度爲60和60

它不能解碼的ImagePath的ImagePath不爲空

Uri imgselect = data.getData(); 
String imgpath = imgselect.getPath(); 
if(imgpath !=null) 
{ 
    Bitmap bm = BitmapFactory.decodeFile(imgpath); 
} 

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, 
      int reqHeight) { 

     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options); 

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

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     Bitmap bmp = BitmapFactory.decodeFile(path, options); 
     return bmp; 
     } 

    public static int calculateInSampleSize(BitmapFactory.Options options, 
      int reqWidth, int reqHeight) { 

     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 
      if (width > height) { 
       inSampleSize = Math.round((float) height/(float) reqHeight); 
      } else { 
       inSampleSize = Math.round((float) width/(float) reqWidth); 
      } 
     } 
     return inSampleSize; 
     } 
+1

我解決了這個問題。我沒有在屏幕上顯示圖像。謝謝回答。 – axmug

+0

完美的解決方案...爲我工作。 –

+0

@sunil kumar可以請你解決我這個問題幾乎相同http://stackoverflow.com/questions/43868432/how-to-set-image-in-imageview-choosing-file-from-gallery-and-giving-null -except?noredirect = 1#comment74772806_43868432 – Andie

0

沒有必要,因爲你指的是內容提供商(它會爲你做這個)使用的實際文件

Uri imgselect = data.getData(); 
Bitmap bm = BitmapFactory.decodeFile(imgselect.getPath()); 

不能檢查,如果一切正常,你可能需要開放的第一解碼。

+0

我嘗試了您輸入的內容,但未運行。我獲得了空解碼位圖。我解碼了Uri,但結果是一樣的。我被卡住了。 – axmug