2011-05-18 66 views
0

我目前使用下面的代码下载,然后显示图像,而我只是希望有人能告诉我如何检查文件是否已经下载并只显示如果是这样,否则,然后下载并显示..请帮助。Android应用开发 - 下载一次,用于显示图像

void downloadFile(String fileUrl){ 
    URL myFileUrl =null; 
    try { 
    myFileUrl= new URL(fileUrl); 
    } catch (MalformedURLException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    try { 
    HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection(); 
    conn.setDoInput(true); 
    conn.connect(); 
    InputStream is = conn.getInputStream(); 

    Bitmap bmImg = BitmapFactory.decodeStream(is); 
    ImageView imView = (ImageView)findViewById(R.id.imview); 
    imView.setImageBitmap(bmImg); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 

回答

0
File imageIamInterestedIn = new File(filePath); 
if (!imageIamInterestedIn.exists()) 
{ 
    download(); 
} 
else 
{ 
    imageView.setImageBitMap(BitmapFactory.decodeFile(filePath)); 
} 
0

你需要以某种方式写高速缓存,无论是临时的内存缓存,或永久数据库/文件缓存。

下面是一些步骤,以达到你想要的东西:

  1. 有人请求他们的设备上观看图像。
  2. 检查您的缓存(内存或持久性,如上所述)。
  3. 如果它存在那里,返回,否则使用您的代码获取图像。

如果您不想再次获取该图像,则需要持久性缓存,因为您将在未来经常访问此图像。如果速度是一个问题,临时内存缓存将是可取的,并且您未来将不会访问此映像。

相关问题