2017-07-06 65 views
1

我試圖從服務器上下載mp3文件。 我成功製作了它,我可以播放該文件,但沒有縮略圖和文件信息,例如藝術家和專輯名稱。Android AsyncTask下載.mp3文件 - 信息和縮略圖

當我直接從chrome下載文件時,我上面提到的信息會被下載。 任何想法如何讓它工作?

這裏是我的代碼:

  URL url = new URL(urlDwn+urlVid); 

      HttpURLConnection connection =(HttpURLConnection) url.openConnection(); 
      connection.connect(); 

      //file length 
      int lengthOfFile = connection.getContentLength(); 
      //intput - read file 
      InputStream inputStream = new BufferedInputStream(url.openStream(), 8192); 

      //output - write file 
      if(true) { 
       System.out.println("Downloading"); 
       OutputStream outputStream = new FileOutputStream(root+"/"+fileName); 
       System.out.println(root+"/"+fileName); 
       byte data[] = new byte[1024]; 
       long total = 0; 
       while ((count = inputStream.read(data)) != -1) { 
        total += count; 
        //progress 
        publishProgress((int) ((total * 100)/lengthOfFile)); 

        //writing to file 
        outputStream.write(data,0, count); 
       } 
       outputStream.flush(); 
       outputStream.close(); 
       inputStream.close(); 
+0

可能問題是您檢索縮略圖和信息的方式? –

+0

我如何檢索它? –

+0

當你通過chrome下載它時,你會以某種方式檢索它。 –

回答

1

我解決它!

似乎縮略圖和mp3標籤(藝術家等)正確下載。問題出在文件管理器本身(或者是Android系統)上,所以當我重新啓動設備時,它就顯示出來了!

編輯

沒有必要重新啓動。您只需將以下幾行添加到onPostExecute方法中,該方法告訴系統已添加新文件。

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
intent.setData(Uri.fromFile(file)); 
sendBroadcast(intent);