显示存储在缓存中的图像,Android

显示存储在缓存中的图像,Android,android,android-layout,android-xml,android-image,Android,Android Layout,Android Xml,Android Image,我从网上下载图片并将它们存储在缓存中。现在,我如何在XML中显示照片。存储的照片如下所示: /data/data/com.example.app/cache/wpta_i.jpeg 根据位置的不同而变化。我的XML如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layou

我从网上下载图片并将它们存储在缓存中。现在,我如何在XML中显示照片。存储的照片如下所示:

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

根据位置的不同而变化。我的XML如下所示:

 <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>


如何从缓存中加载图像并显示它?

您应该从代码中加载可绘制图像,然后将ImageDrawable设置为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);

您应该从代码中加载drawable,然后将ImageDrawable设置为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);
您可以使用获取缓存目录,然后从代码中访问它,如Perroloco的回答所示

文件中明确警告:

当设备存储空间不足时,这些文件将首先被删除

无法保证这些文件将在何时被删除

因此,在尝试加载之前,请确保您的文件存在。

您可以使用获取缓存目录,然后从代码中访问它,如Perroloco的回答所示

文件中明确警告:

当设备存储空间不足时,这些文件将首先被删除

无法保证这些文件将在何时被删除


因此,在尝试加载之前,请确保您的文件存在。

hi您能告诉我如何清除缓存吗??我有这样的问题。我从w/s中获取图像,但当图像在w/s中更改时。我获取新图像,但无法显示新图像代替旧图像。那么我如何清除缓存?请看这个问题:嗨,你能告诉我如何清除缓存吗??我有这样的问题。我从w/s中获取图像,但当图像在w/s中更改时。我获取新图像,但无法显示新图像代替旧图像。那么我如何清除缓存?看看这个问题:这是一个不同的问题,已经回答了很多次。这是一个不同的问题,已经回答了很多次,例如