有个奇怪的问题。Url: https://s3.amazonaws.com/myappdata/msg/171401089927.mp3(不再可用)的文件在PC上下载ok及其mp3文件。但是当我尝试在Android FOA上DL它时,我得到内容类型"application/xml"而不是"audio/mpeg",当下载开始时,我得到:
05-30 12:13:44.478: E/PlayerService(28023): java.io.FileNotFoundException: https://s3.amazonaws.com/myappdata/msg/171401089927.mp3
05-30 12:13:44.478: E/PlayerService(28023): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
05-30 12:13:44.478: E/PlayerService(28023): at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
用于DL的代码:
/**
* Download the url stream to a temporary location
*/
public void downloadAudioIncrement(String mediaUrl) throws IOException {
Log.i(TAG, "downloadAudioIncrement(): mediaUrl: "+mediaUrl+"ncacheDir: "+cacheDir);
URL url = null;
try {
url = new URL(mediaUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
throw new IOException("Unable to create InputStream for mediaUrl:" + mediaUrl);
}
// this file will represent whole downloaded song
mp3FileDownloaded = new File(cacheDir, mp3FileName);
if (!mp3FileDownloaded.exists())
//FileUtils.makeDirsForFile(mp3FileDownloaded);
try{
mp3FileDownloaded.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
if (!mp3FileDownloaded.canWrite())
throw new IOException("Can't open temporary file for writing");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setReadTimeout(1000 * 20);
urlConnection.setConnectTimeout(1000 * 5);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
int mp3BytesSize = urlConnection.getContentLength();
// final String
// contentLengthStr=urlConnection.getHeaderField("content-length");
String ctype = urlConnection.getContentType();
if (ctype == null) {
ctype = "";
} else {
ctype = ctype.toLowerCase(Locale.US);
}
// See if we can handle this type
Log.i(TAG, "Content Type: " + ctype);
if ( ctype.contains("audio/mpeg") || TextUtils.isEmpty(ctype) ) {
String temp = urlConnection.getHeaderField(BITRATE_HEADER);
Log.i(TAG, "Bitrate: " + temp);
// if (temp != null){
// bitrate = new Integer(temp).intValue();
// }
} else {
Log.e(TAG, UNSUPPORTED_AUDIO_TYPE+": " + ctype);
// throw new IOException(UNSUPPORTED_AUDIO_TYPE+": " + ctype);
// Log.e(TAG, "Or we could not connect to audio");
// stop();
// return;
}
final InputStream stream = new BufferedInputStream(urlConnection.getInputStream(),8192);
...
就在最后显示的代码行(实例化InputStream流)处引发了上述ioexception。还有其他mp3文件存在于同一位置,他们正在下载没有任何问题,但只有上面提到的url失败。
这里有什么问题吗?
这个问题出现在HTC Rezound的AOS 4.0.4上。在其他设备上,使用AOS 2.3.5一切正常
看起来像
urlConnection.setDoOutput(true);
是问题的根源,因为我没有上传任何数据。一切都很好,因为我评论它。此外,这些FileNotFoundException而获得InputStream对象从HttpURLConnection和Android HttpURLConnection gettinputstream抛出NullPointerException线程可能是有帮助的。