2011-11-08 55 views
0

我正在将设备相机中的图像保存到SD卡上的目录(例如:/sdcard/appName/image.jpg),然后将路径保存到数据库中。我的问题是,我似乎无法使用游标适配器将图像加载到ListView如何从文件路径设置图像?

我试过了下面的代码,其中helper.getImg();是一个来自我的数据库帮助程序的方法,它返回String(文件路径),但它不起作用。

icon=(ImageView)row.findViewById(R.id.icon_pura); 
String imgPath=helper.getImg(c); 
Bitmap myBitmap=BitmapFactory.decodeFile(imgPath); 
icon.setImageBitmap(myBitmap); 

回答

0

答案与URI有关,您需要使用externalstorage()函数。使用集路径将不是每一个设备上工作

反正你存储路径为URI是在这条道路

0
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Directory name/"; 
File file = new File(filepath,imagename); 
FileInputStream fs = null; 
try 
{ 
    fs = new FileInputStream(file); 
} 
catch (FileNotFoundException e) { 
// TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

BitmapFactory.Options bfOptions = new BitmapFactory.Options(); 
      /* 
      * bfOptions.inDither=false; //Disable Dithering mode 
      * bfOptions.inPurgeable=true; //Tell to gc that whether it needs 
      * free memory, the Bitmap can be cleared 
      * bfOptions.inInputShareable=true;*/ 

bfOptions.inJustDecodeBounds = false; 
bfOptions.inTempStorage = new byte[32 * 1024]; 
try { 
    Bitmap originalImage = BitmapFactory.decodeFileDescriptor(fs.getFD(), null,bfOptions); 
    icon.setImageBitmap(originalImage); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
       e.printStackTrace(); 
} 
0

如果按照人士Himanshu的(正确的)建议检索和parsings项目更加灵活,使确保如果你打算让用户选择加载和重新加载图像,请确保手动设置icon.setImageBitmap(null);负载之间,因为否则Android会泄漏该内存,你会崩溃你的应用程序。它不是100%一致的,并且与您正在加载的图像的大小有关,但是前几天我发现这个泄漏,并且100%确定它在那里。

相关问题