开发者

Load and unload images

开发者 https://www.devze.com 2023-02-24 17:35 出处:网络
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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号