我正在尝试从服务器下载 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();
最佳答案
我解决了!
缩略图和 mp3 标签(艺术家等)似乎已正确下载。问题出在文件管理器本身(或者可能是 Android 系统),所以当我重新启动设备时,它自己显示出来了!
编辑
无需重启。您需要做的就是将下面的行添加到 onPostExecute 方法,它会告诉系统已添加新文件。
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
关于java - Android AsyncTask 下载 .mp3 文件 - 信息和缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44947612/