0
如何加载和卸载SD卡上的图像到imageview?加载和卸载图像
如何加载和卸载SD卡上的图像到imageview?加载和卸载图像
试试这个,它假定你知道你的图像文件的路径:
ImageView img = (ImageView)findViewById(R.id.myimageview);
img.setBackgroundDrawable(Drawable.createFromPath("path/to/your/file"));
首先,你需要找到保存/载入文件夹:
public File getDataFolder(Context context) {
File dataDir = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
dataDir = new File(Environment.getExternalStorageDirectory(), "myappdata");
if(!dataDir.isDirectory()) {
dataDir.mkdirs();
}
}
if(!dataDir.isDirectory()) {
dataDir = context.getFilesDir();
}
return dataDir;
}
然后,你可以加载您的文件夹中的图像:
File cacheDir = GlobalClass.instance().getCacheFolder(this);
File cacheFile = new File(cacheDir, wallpaperFilePath);
InputStream fileInputStream = new FileInputStream(cacheFile);
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = scale;
bitmapOptions.inJustDecodeBounds = false;
Bitmap wallpaperBitmap = BitmapFactory.decodeStream(fileInputStream, null, bitmapOptions);
ImageView imageView = (ImageView)this.findViewById(R.id.preview);
imageView.setImageBitmap(wallpaperBitmap);
您也可以查看这个原始示例。它提供了一个非常有用的功能来保存和加载SD卡中的图像。 Android Save And Load Downloading File Locally
这里有一个类似的问题:http://stackoverflow.com/questions/2688169/how-to-load-an-imageview-from-a-png-file – bigstones 2011-04-13 14:34:01