Load and unload images
How can I load and unload images from the SD card to t开发者_运维百科he imageview?
Try this, it assumes you know the file path of your image:
ImageView img = (ImageView)findViewById(R.id.myimageview);
img.setBackgroundDrawable(Drawable.createFromPath("path/to/your/file"));
First, you need to find the save/load file folder:
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;
}
Then, you can load your image from your file folder:
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);
You can also check this original example. It provides a very useful functions to save and load images in SD card. Android Save And Load Downloading File Locally
精彩评论