Java 如何使用android从URL下载音频
我是android新手,对于培训,我想创建一个从现有api下载音频的应用程序 我正在获取从api下载文件的链接,但无法正确下载 我试过下载管理器,虽然它说下载了,但它甚至没有下载 以下是主要代码:Java 如何使用android从URL下载音频,java,android,audio,android-download-manager,Java,Android,Audio,Android Download Manager,我是android新手,对于培训,我想创建一个从现有api下载音频的应用程序 我正在获取从api下载文件的链接,但无法正确下载 我试过下载管理器,虽然它说下载了,但它甚至没有下载 以下是主要代码: public void Download(String link) { downloadManager = (DownloadManager)m_context.getSystemService(m_context.DOWNLOAD_SERVICE); Uri Download_
public void Download(String link)
{
downloadManager = (DownloadManager)m_context.getSystemService(m_context.DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(link);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
//Restrict the types of networks over which this download may proceed.
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
//Set whether this download may proceed over a roaming connection.
request.setAllowedOverRoaming(false);
//Set the title of this download, to be displayed in notifications (if enabled).
request.setTitle("Downloading " + m_name);
//Set a description of this download, to be displayed in notifications (if enabled)
request.setDescription("Downloader");
//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalFilesDir(m_context, Environment.DIRECTORY_DOWNLOADS, m_name +".mp3");
//Enqueue a new download and same the referenceId
downloadReference = downloadManager.enqueue(request);
}
m_上下文是主要活动
链接是包含要下载的文件的url。
我正在使用广播接收器查看下载是否完成,所以我尝试打开它,但什么也没发生
我也尝试过在没有下载管理器的情况下下载它,尽管它下载了,但大小非常小(1-15KB),但有时(不是大多数时间,可能是5%)它会提供完整的音频(根据大小和时间)
代码如下:
public void Download(final String link)
{
try {
Thread t= new Thread(){
public void run() {
int count;
try {
URL url = new URL(link);
URLConnection conexion = url.openConnection();
Thread.sleep(20000);
conexion.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conexion.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/Music",m_name+".mp3");
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
} catch (Exception e) {
downloadSuccess = false;
e.printStackTrace();
}
}
};
t.start();
t.join();
} catch (InterruptedException e) {
downloadSuccess = false;
e.printStackTrace();
}
}
您将看到thread.sleep,我假设响应需要一点时间来处理,但即使是一分钟也没有完成
任何指导都将不胜感激
多谢各位
更新:
有一种称为GZIPInputStream的东西,在gzip中对未能正确下载的文件进行编码
代码的和平:
if ("gzip".equals(httpConn.getContentEncoding())) {
BufferedReader in= new BufferedReader(new InputStreamReader(new GZIPInputStream(httpConn.getInputStream())));
StringBuilder sb = new StringBuilder();
String read;
while ((read = in.readLine()) != null){
sb.append(read);
}
}
它仍然不能帮助我以正确的方式下载,但我认为这是一条正确的道路,而且任何帮助都会很好。多谢各位