我当前的Android应用程序下载了许多音频文件.当我使用此代码执行下载时,出现找不到文件的异常:
try { final URL downloadfileUrl = new URL("http://filelocation/url.m4a"); final httpURLConnection httpURLConnection = (httpURLConnection) downloadfileUrl.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setDoOutput(true); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setReadTimeout(10000); httpURLConnection.connect(); mTrackDownloadfile = new file(Record.this.getCacheDir(), "mediafile"); mTrackDownloadfile.createNewfile(); final fileOutputStream fileOutputStream = new fileOutputStream(mTrackDownloadfile); final byte buffer[] = new byte[16 * 1024]; final inputStream inputStream = httpURLConnection.getinputStream(); int len1 = 0; while ((len1 = inputStream.read(buffer)) > 0) { fileOutputStream.write(buffer, 0, len1); } fileOutputStream.flush(); fileOutputStream.close();} catch (final Exception exception) { Log.i(TAG, "doInBackground - exception" + exception.getMessage()); exception.printstacktrace(); mTrackDownloadfile = null;}
当我使用此代码时,它可以正常工作:
try { final URL downloadfileUrl = new URL("http://filelocation/url.m4a"); final URLConnection urlConnection = downloadfileUrl.openConnection(); mTrackDownloadfile = new file(PlayOpponent.this.getCacheDir(), "mediafile"); mTrackDownloadfile.createNewfile(); final fileOutputStream fileOutputStream = new fileOutputStream(mTrackDownloadfile); final byte buffer[] = new byte[16 * 1024]; final inputStream inputStream = urlConnection.getinputStream(); int len1 = 0; while ((len1 = inputStream.read(buffer)) > 0) { fileOutputStream.write(buffer, 0, len1); } fileOutputStream.flush(); fileOutputStream.close();} catch (final Exception exception) { Log.i(TAG, "doInBackground - exception" + exception.getMessage()); exception.printstacktrace(); mTrackDownloadfile = null;}
有人可以指出我要去哪里了吗?
解决方法:
根据这个blog去除
httpURLConnection.setDoOutput(true);
在您的代码中可以解决问题.据说这是ICS问题.
总结以上是内存溢出为你收集整理的android应用下载文件全部内容,希望文章能够帮你解决android应用下载文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)