0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nick Robertson
  • 1,047
  • 4
  • 18
  • 41

2 Answers2

3

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);
Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53
  • hii can you tell me how can i clear the cache?? i have problem like that.i am getting the images from w/s but when the image is changed in w/s.i am getting the new images but can't show the new in place of old.so how can i clear cache? – Google Oct 09 '12 at 11:19
  • 1
    look at this question: http://stackoverflow.com/questions/6134103/clear-applications-data-programatically – Sebastian Breit Oct 09 '12 at 11:28
0

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.

Community
  • 1
  • 1
Ben Weiss
  • 17,182
  • 6
  • 67
  • 87
  • This is a different question that has been answered many times, for example [here](http://stackoverflow.com/a/10069679/422060). – Ben Weiss Oct 09 '12 at 11:23