当我尝试获取图像时,BitmapFactory.decodeFile()返回null

时间:2013-02-22 07:40:54

标签: android bitmapfactory

 String imageInSD = Environment.getExternalStorageDirectory()+"/DCIM/Soovy/2089.jpg";         
 Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);

 if(bitmap == null){                  
     Log.v("combine image", "null");
 }else{ 
     Log.v("combine image", "not null");                  
 }

更新:使用了Environment.getExternalStorageDirectory()感谢信息

2 个答案:

答案 0 :(得分:1)

请勿直接访问SD卡,尝试通过Environment访问它。

像这样:

String imageDir = Environment.getExternalStorageDirectory()+"/DCIM/Soovy/2089.jpg";

Bitmap bitmap = BitmapFactory.decodeFile(imageDir);

if(bitmap == null){                  
     Log.v("combine image", "null");
 }else{ 
     Log.v("combine image", "not null");                  
 }

答案 1 :(得分:0)

试试这个:

String imageInSD = "file:///mnt/sdcard/DCIM/Soovy/2089.jpg";        

但是你不应该硬编码路径。使用

Environment.getExternalStorageState()

获取sdcard的根目录。

String imageInSD = Environment.getExternalStorageState() + "/DCIM/Soovy/2089.jpg";