我想从服务器下载文件。文件已在我的 android 设备中成功创建,但该文件是空文件。它没有在文件中写入任何内容。
我的文件读取代码如下:
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//connect
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory();
//create a new file, to save the downloaded file
File file = new File(SDCardRoot,pos);
FileOutputStream fileOutput = new FileOutputStream(file);
//Stream used for reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file which we are downloading
totalSize = urlConnection.getContentLength();
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
}
fileOutput.close();
}
我已经调试了代码。while ( (bufferLength = inputStream.read(buffer)) > 0 ) 条件有问题,因为编译器没有进入这个循环。
我的日志猫错误:
ERROR/ActivityThread(8218): Activity com.sec.android.app.myfiles.DetailsActivity has leaked IntentReceiver com.sec.android.app.myfiles.DetailsActivity$4@42760570 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.sec.android.app.myfiles.DetailsActivity has leaked IntentReceiver com.sec.android.app.myfiles.DetailsActivity$4@42760570 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:792)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1254)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1241)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1235)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:372)
at
com.sec.android.app.myfiles.DetailsActivity.setLocaleReceiver(DetailsActivity.java:185)
at com.sec.android.app.myfiles.DetailsActivity.onCreate(DetailsActivity.java:121)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)