2017-07-06 67 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);