要把服务器那到的图片保存到本地,图片显示没有问题,保存时保错
这是报的错java.io.IOException: open failed: EACCES (Permission denied)
权限该写的应该都写了吧:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAG"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<permission android:name="android.permission.WRITE_MEDIA_STORAGE" />
下面是代码:如果路径用第5行的,10行显示false,文件夹没有创建成功
如果用第6行,13行 显示路径没有问题,但是17行报上述错误
private boolean saveToLocal(){
boolean result = false;
String fileName = MD5Converter.getMD5(currentImage) + ".jpg";
String path = Environment.getExternalStorageDirectory() + "/savedImage/";
//String path = Environment.getExternalStorageDirectory() + "/";
File folder = new File(path);
if (!folder.exists()) {
boolean b = folder.mkdirs();
Toast.makeText(getApplicationContext(), b+"", 1).show();
}
File file = new File(path + fileName);
Toast.makeText(getApplicationContext(), file.getAbsolutePath(), 1).show();
if (!file.exists()) {
try {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
file.createNewFile();
// BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
// bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
// bos.flush();
// bos.close();
// Toast.makeText(getApplicationContext(), "Saved!", 1).show();
Toast.makeText(getApplicationContext(), file.getPath(), 1).show();
}else{
Toast.makeText(getApplicationContext(), "SD card error", 1).show();
}
//update gallery
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
this.sendBroadcast(intent);
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
跪求大神帮忙啊。。不懂
别的地方对不对先不管了。。我也不知道。。现在是没权限创建文件。。file explorer可以创建文件夹(创建了还不让删。。),托文件进去也可以,应该也不是sd开始的问题。。急。。。
------解决思路----------------------
无语了,复制个文本还丢三落四的.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
------解决思路----------------------
我还给他调试了下 纠结好一阵子了 结果还真如一楼所说....二年级生表示鄙视楼主