我正在尝试创建一个可以下载音乐文件的应用。
如何从URL下载android文件中的mp3文件并将其保存在SD卡中?
我使用此代码但lenghtOfFile
总是<=350
:
String fileUrl = (String) params[0];
path = (String) params[1];
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
int count;
try {
URL url = new URL(fileUrl);
URLConnection connection = url.openConnection();
connection .connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = connection .getContentLength();
// downlod the file
input = new BufferedInputStream(url.openStream());
output = new FileOutputStream(path);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int)(total*100/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
url文件:
http://snd1.tebyan.net/1391/12/08_Khaneh_D_117323.mp3
http://snd1.tebyan.net/1393/12/100_Pedar_Va_Madar_D_148797.mp3
and .....
更新:
int code= connection.getResponseCode();\\302
String _result= connection.getResponseMessage();\\ found
注意:
文件会在Samsung Galaxy S3
下载,但文件不会在Samsung tablet N8000
答案 0 :(得分:0)
你可以实现HMTL5并从那里开始,可能需要一些JS ......
答案 1 :(得分:0)
对于302响应,您需要connection.setInstanceFollowRedirects(true)
才能关注它。在connection.connect()
之前