2013-08-29 94 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