如何将图像从SD卡加载和卸载到imageview?
答案 0 :(得分:1)
试试这个,它假定您知道图像的文件路径:
ImageView img = (ImageView)findViewById(R.id.myimageview);
img.setBackgroundDrawable(Drawable.createFromPath("path/to/your/file"));
答案 1 :(得分:0)
首先,您需要找到保存/加载文件夹:
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