2017-04-19 57 views
0

我有一个聊天应用程序,让用户通过Firebase发送图像,视频等,当其他用户开始与Firebase数据库同步时,它首先将文件下载到本地存储中,如其他应用程序Whatapp,Messenger),然后从那里玩。Android-避免文件从Firebase下载一次以上

但我的问题是每当用户活动刷新所有文件一次又一次下载并使相同的文件的副本。我怎样才能避免这种情况。

这里是我从AsyncTask下载Firebase存储的文件。

public class DownloadAttachment extends AsyncTask<Void, String, String> { 
    String DownloadUrl,fileName; 
    File file; 
    Context context; 
    ProgressBar progressBar; 
    public static final String TAG="###Download Attachment"; 

    public DownloadAttachment(Context context, String downloadUrl, String fileName) { 
     DownloadUrl = downloadUrl; 
     this.fileName = fileName; //UUID.randomUUID().toString() 
     this.context=context; 
    } 


    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     int count; 
     try { 
      File root = android.os.Environment.getExternalStorageDirectory(); 

      Log.d(TAG,"DO IN BACKGROUND RUNNING"); 
      File dir = new File(root.getAbsolutePath() + "/Downloaded Files/"); 
      if (dir.exists() == false) { 
       dir.mkdirs(); 
      } 


      URL url = new URL(DownloadUrl); //you can write here any link 
      file = new File(dir, fileName); 

      long startTime = System.currentTimeMillis(); 
      Log.d(TAG, "download begining"); 
      Log.d(TAG, "download url:" + url); 
      Log.d(TAG, "downloaded file name:" + fileName); 

     /* Open a connection to that URL. */ 
      URLConnection ucon = url.openConnection(); 

      //this will be useful so that you can show a typical 0-100% progress bar 
      int lengthOfFile=ucon.getContentLength(); 
     /* 
     * Define InputStreams to read from the URLConnection. 
     */ 
      InputStream is = ucon.getInputStream(); 
      BufferedInputStream bis = new BufferedInputStream(is); 

     /* 
     * Read bytes to the Buffer until there is nothing more to read(-1). 
     */ 
      ByteArrayOutputStream baf = new ByteArrayOutputStream(5000); 
      int current = 0; 
      long total=0; 
      while ((current = bis.read()) != -1) { 
       baf.write((byte) current); 
       total=total+current; 
       //PUBLISH THE PROGRESS 
       //AFTER THIS onProgressUpdate will be called 
       publishProgress(""+(int)(total*100)/lengthOfFile); 
      } 


     /* Convert the Bytes read to a String. */ 
      FileOutputStream fos = new FileOutputStream(file); 
      fos.write(baf.toByteArray()); 
      fos.flush(); 
      fos.close(); 
      Log.d("DownloadManager", "download ready in " + ((System.currentTimeMillis() - startTime)/1000) + " sec"); 
      Log.d(TAG,"File Path "+file); 

     } catch (IOException e) { 
      Log.d("DownloadManager", "Error: " + e); 
     } 

     return file.toString(); 
    }  

} 

我无法检查文件的名称因为我使用UUID生成文件的随机名称。

请指导我这个问题

回答

1

在我看来,你可以保持下载文件表,在本地SQLite数据库,包含两列:

  • TAG:文件名或的一些独特的ID文件
  • URI:存储在设备上的 文件的URI /路径。

在下载任何文件之前,您应该使用TAG列检查其表项是否存在于表格中。如果确实如此,则不要从firebase下载,而是使用URI从设备中获取它。如果不是,那么您可以下载该文件并在表中为该文件插入一行。

+0

你可能是对的,但它有可能我们应该等待一些更多的解决方案,然后再接受这个答案 – Ritu