netboynew 2016-12-30 07:12 采纳率: 40%
浏览 883
已采纳

android从网络上下载图片,写入手机时错误?求解

 String s = params[0];

// Toast.makeText(activity,s,Toast.LENGTH_SHORT).show();
try {
URL url = new URL(s);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //打开连接
int size = conn.getContentLength();//获取文件大小
//0标记表示需要更新最大进度值,1表示更新当前下载的进度取
publishProgress(0,size);
byte[] bytes = new byte[20];
int len = -1;
InputStream inputStream = conn.getInputStream(); //读取数据流
FileOutputStream fileOutputStream = new FileOutputStream("/storage/emulated/0/"+ System.currentTimeMillis()+".jpg");
while ((len=inputStream.read(bytes)) != -1){
fileOutputStream.write(bytes,0,len);
publishProgress(1,len); //更新进度
fileOutputStream.flush();
// Thread.sleep(500);
}
fileOutputStream.close();
inputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

                    提示以下错误:java.io.FileNotFoundException: /storage/emulated/0/1483081566430.jpg: open failed: EACCES (Permission denied)
                    本人使用小米5s真机, <uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>权限都添加了。求解
  • 写回答

5条回答 默认 最新

  • ytmfdw 2016-12-30 07:21
    关注

    你的目录用错了,不能直接"/storage/emulated/0/"这样写,用
    Environment.getExternalStorageDirectory()来获取Sdcard根目录
    另外,需要加上写Sdcard的权限 ,你只是加了read_extranal_stroage,需要加上write_extranal_stroage

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考