moe
moe

Reputation: 247

Load and unload images

How can I load and unload images from the SD card to the imageview?

Upvotes: 0

Views: 518

Answers (2)

James
James

Reputation: 5159

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

Upvotes: 0

Twobard
Twobard

Reputation: 2583

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"));

Upvotes: 1

Related Questions