如何从文件路径设置图像?

时间:2011-11-08 17:31:23

标签: android android-imageview android-image

我将图像从设备的相机保存到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);

2 个答案:

答案 0 :(得分:0)

答案与URI有关,您需要使用externalstorage()函数。使用设置路径不适用于每个设备

无论如何,您将路径存储到URI中,这样可以更灵活地检索和解析该路径中的项目

答案 1 :(得分: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();
}