Nick Robertson
Nick Robertson

Reputation: 1047

Display images stored in cache, Android

I download pictures from the Web and I stored them in the cache. Now, how I can display the photo in the XML. The photos stored as follwing:

/data/data/com.example.app/cache/wpta_i.jpeg

Changes according to the position. My XML is as following:

 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
      android:id="@+id/img"  
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center">      
    </ImageView>
  </LinearLayout>

How can I load the image from the cache and display it?

Upvotes: 0

Views: 3304

Answers (2)

Ben Weiss
Ben Weiss

Reputation: 17922

You can use Context.getCacheDir() to get your cache directory and then access it from Code as shown in Perroloco's answer.

The documentation states a clear warning:

These files will be ones that get deleted first when the device runs low on storage.

There is no guarantee when these files will be deleted.

So make sure that your file exists prior to trying to load it.

Upvotes: 0

Sebastian Breit
Sebastian Breit

Reputation: 6159

You should load the drawable from code and then setImageDrawable to the ImageView.

String pathName = "/data/data/com.example.app/cache/wpta_i.jpeg"; 
Drawable d = Drawable.createFromPath(pathName);
ImageView myImageView=(ImageView)findViewById(R.id.img);
myImageView.setImageDrawable(d);

Upvotes: 3

Related Questions