我需要在android上保存一张相机上的图像。我在清单中使用了写外部存储权限,并且使用以下代码
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()相同的结果。
知道密码出了什么问题吗?
谢谢
发布于 2016-12-28 15:13:54
对于那些不像我这样有经验的人。我和这个问题斗争了一段时间,失去了头发。我的目标是api 21 (为了兼容起见),它在棒棒糖上工作,但在棉花糖上却不会创建目录。我在舱单上确实有“使用”许可,但还是不能用。显然,在Marshmallow,当你用Android工作室安装时,它从来不问你是否应该允许它,它只是悄悄地失败了,就像你否认的那样。您必须进入“设置”、“应用程序”、“选择应用程序”并打开权限开关。
发布于 2020-04-22 08:26:32
一个像我一样在Android10尝试的人。请在清单中使用以下API:
<application android:requestLegacyExternalStorage="true" ... >
...
</application> 谷歌最新更新:当你更新你的应用程序后,你的安卓11,系统忽略了requestLegacyExternalStorage标志。
发布于 2017-02-20 14:43:41
你有没有把
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />在你的AndroidManifest里?如果您使用的是android,您必须请求用户许可才能在sd上编写,请看这里的示例
https://stackoverflow.com/questions/40087355
复制相似问题