3-6运行报错
package com.example.handlderproject;
import android.app.Activity;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class DownloadActivity extends Activity {
private Button button;
private ProgressBar progressBar;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download);
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
download("http://download.sj.qq.com/upload/connAssitantDownload/upload/MobileAssistant_1.apk");
}
}).start();
}
});
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==100)
{
progressBar.setProgress((Integer) msg.obj);
}
}
};
}
private void download(String s) {
try {
URL url=new URL(s);
HttpURLConnection urlc= (HttpURLConnection) url.openConnection();
int downloadleng=urlc.getContentLength();
InputStream is=urlc.getInputStream();
String downloadfilename= Environment.getExternalStorageDirectory()
+ File.separator + "imooc" + File.separator;
File file=new File(downloadfilename);
int downloadsize=0;
if(!file.exists())
{
file.mkdir();
}
String downloadoutname=downloadfilename+"imooc.apk";
File apkFile = new File(downloadoutname);
if(apkFile.exists()){
apkFile.delete();
}
OutputStream ops=new FileOutputStream(downloadoutname);
int len=0;
byte[] b=new byte[1024];
while((len=is.read(b))!=-1){
ops.write(b,0,len);
downloadsize+=len;
Message message=Message.obtain();
message.what=100;
message.obj=downloadsize*100/downloadleng;
handler.sendMessage(message);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
报错内容
11-22 06:42:51.230 4043-4135/com.example.handlderproject D/NetworkSecurityConfig: No Network Security Config specified, using platform default
11-22 06:42:51.421 4043-4135/com.example.handlderproject W/System.err: java.io.FileNotFoundException: /storage/emulated/0/imooc/imooc.apk (No such file or directory)
11-22 06:42:51.421 4043-4135/com.example.handlderproject W/System.err: at java.io.FileOutputStream.open(Native Method)
11-22 06:42:51.421 4043-4135/com.example.handlderproject W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
11-22 06:42:51.421 4043-4135/com.example.handlderproject W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:108)
11-22 06:42:51.422 4043-4135/com.example.handlderproject W/System.err: at com.example.handlderproject.DownloadActivity.download(DownloadActivity.java:77)
11-22 06:42:51.422 4043-4135/com.example.handlderproject W/System.err: at com.example.handlderproject.DownloadActivity.access$000(DownloadActivity.java:23)
11-22 06:42:51.422 4043-4135/com.example.handlderproject W/System.err: at com.example.handlderproject.DownloadActivity$1$1.run(DownloadActivity.java:38)
11-22 06:42:51.422 4043-4135/com.example.handlderproject W/System.err: at java.lang.Thread.run(Thread.java:761)
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星