Android mkdirs无法正常工作 [英] android mkdirs not working

查看:900
本文介绍了Android mkdirs无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Android上的相机保存图像. 我在清单中使用了写入外部存储权限,并且正在使用此代码

i need to save an image from camera on android. i used the write external storage permission in manifest and i am using this code

File dir = new File(Environment.getExternalStorageDirectory(), "Test");
if (!dir.exists() || !dir.isDirectory())
    dir.mkdirs();

String path = dir.getAbsolutePath();
Log.d(TAG, path);                     //log show the path
File file = new File(dir.getAbsolutePath() + "/Pic.jpg");
Log.d(TAG, file.getAbsolutePath());   //again path is shown here

outStream = new FileOutputStream(file);
outStream.write(bytes);
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + bytes.length);   //fail here
} catch (FileNotFoundException e) {
Log.d(TAG, "not done");                       //error is here (this exception is thrown)
} catch (IOException e) {
Log.d(TAG, "not");
} finally {  }

我也尝试了mkdir()而不是mkdirs()相同的结果.

i also tried mkdir() instead of mkdirs() same result.

知道代码中出了什么问题吗?

any idea what went wrong in the code?

谢谢

推荐答案

针对那些没有我这样经验丰富的人.我为这个问题而斗争,掉头发了一段时间.我的目标是api 21(出于兼容性考虑),它可以在棒棒糖上工作,但在棉花糖上却无法创建目录.我确实在清单中具有使用"权限,但仍然无法使用.显然,在棉花糖中安装Android Studio时,它从不询问您是否应该授予它许可,而只是悄悄地失败了,就像您拒绝了它一样.您必须进入设置",应用",选择您的应用,然后打开权限开关.

For those not as experienced like me. I fought this issue, lost hair for some time. I am targeting api 21 (for compatibility sake) and it worked on lollipop but on marshmallow it would not create the directory. I did have the "uses" permission in the manifest but it still would not work. Apparently in Marshmallow when you install with Android studio it never asks you if you should give it permission it just quietly fails, like you denied it. You must go into Settings, apps, select your application and flip the permission switch on.

这篇关于Android mkdirs无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆