我试图从URL下载mp3文件并将SD保存在音乐文件夹中。 但无论我做什么,都不会将它保存在SD上,它只是下载它,然后在试图找到它时,它没有发现它。
这是我在异步任务类中的代码:
protected String doInBackground(String... params) {
try{
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
File sd = Environment.getExternalStorageDirectory();
File file = new File(sd, "TestSongs.mp3");
FileOutputStream outputStream = new FileOutputStream(file);
InputStream inputStream = connection.getInputStream();
int totalsize = connection.getContentLength();
int downloadSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0;
while((bufferLength = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0 , bufferLength);
downloadSize += bufferLength;
publishProgress("" + (int) ((downloadSize * 100) / totalsize));
}
outputStream.close();
}catch(Exception e) {
Log.d(TAG, e.getMessage());
}
return null;
}
答案 0 :(得分:0)
添加
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在您的清单中的某个位置,<application></application>
标记