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