最后的图片在真机上无法显示
代码和老师的一毛一样!
public void run() {
try {
URL httpUrl=new URL(url);
//通过URL对象获取连接
HttpURLConnection conn= (HttpURLConnection) httpUrl.openConnection();
//设置 连接读取时长
conn.setReadTimeout(5000);
//设置以get形式请求
conn.setRequestMethod("GET");
//文档上只是写着设置连接允许输入,不知道是什么意思
conn.setDoInput(true);
InputStream in=conn.getInputStream();
FileOutputStream out=null;
File downloadFile=null;
Log.i("info","进入到HttpThread中");
String fileName=String.valueOf(System.currentTimeMillis());
//只有在SD卡状态为MEDIA_MOUNTED时,/mmt/sdcard目录才是可读可写的,并且可以创建目录及文件
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
//获取外部存储路径,外部存储不一定是sd卡,还包括手机自带的内存,内部存储是指data/data/包名下的
File parent=Environment.getExternalStorageDirectory();
//在获取的目录下新建文件名为fileName的文件
downloadFile=new File(parent,fileName);
Log.i("info","创建成功");
//文件输出流
out=new FileOutputStream(downloadFile);
}
byte[] b=new byte[2*1024];
int len;
if(out!=null){
//通过字节流的读取方法,以数组为缓存
while((len=in.read(b))!=-1){
out.write(b,0,len);
}
}
//通过指定sd卡图片文件路径来获取bitmap
final Bitmap bitmap= BitmapFactory.decodeFile(downloadFile.getAbsolutePath());
handler.post(new Runnable() {
@Override
public void run() {
Log.i("info","写入成功");
imageView.setImageBitmap(bitmap);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
在真机上运行完显示这些错误:
I/HwPointEventFilter: do not support AFT because of no config
I/info: 进入到HttpThread中
I/info: 创建成功
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/1496842425984 (Permission denied)
W/System.err: at java.io.FileOutputStream.open(Native Method)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
W/System.err: at com.xiejinhui.httpurlconnection.HttpThread.run(HttpThread.java:55)
I/OpenGLRenderer: Initialized EGL, version 1.4
W/OpenGLRenderer: load: so=libhwuibp.so
dlopen failed: library "libhwuibp.so" not found
W/OpenGLRenderer: Initialize Binary Program Cache: Load Failed
E/OpenGLRenderer: allen debug liyu Key: 0
E/OpenGLRenderer: allen debug liyu Key: 1
E/OpenGLRenderer: allen debug liyu Key: 240518168576
E/OpenGLRenderer: allen debug liyu Key: 34359738371
萌新也看不懂,求大佬帮帮我!
顺便说一下,我的真机没有内存卡用的是自带内存,目录为/storge/emulated/0