【Android报错】FileNotFoundException open failed:文件路径 EPERM (Operation not permitted)外部存储至根目录报错,Android外部存储权限动态获取问题
报错:FileNotFoundException open failed: XXXXXXX EPERM (Operation not permitted)
查了下,大概原因是因为权限的问题。(小白学Android,Android文档查起来还有点吃力,就直接谷歌+百度。)
但是Manifest里该加的权限都加了。还application增加了android:requestLegacyExternalStorage="true",表示没用。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" tools:ignore="ProtectedPermissions" />
Android 6.0以后,Android对权限管理更严格了。试了几种解决办法,都不算我想要的。本意是想存储到SD卡的根路径下。
算了,先忽略了。继续往下学习吧。
解决:
自定义存储方法(原代码)
public void saveIMG(){
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
//获取APP内存储路径,模拟器可行
// ContextWrapper cw = new ContextWrapper(getApplicationContext());
// File sdkPath = cw.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
//获取public存储路径,模拟器可行。真机需要动态权限
// File sdkPath=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//获取根路径,不可行。增加动态获取外部存储读取权限后,真机运行,可行。
File sdkPath=Environment.getExternalStorageDirectory();
Log.v("hehe","sdkPath:"+sdkPath.getPath());
file =new File(sdkPath,"test.jpeg");
//读取图片
@SuppressLint("ResourceType") InputStream is= getResources().openRawResource(R.drawable.kelala);
//存储图片
OutputStream io=new FileOutputStream(file);
bis=new BufferedInputStream(is);
bos=new BufferedOutputStream(io);
int len=0;
byte[] buf=new byte[1024];
while ((len=bis.read(buf))!=-1){
bos.write(buf,0,len);
bos.flush();
}
Log.v("hehe","读取和存储buffer");
Toast.makeText(OutStore.this,"存储成功!!!!!",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
if (bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
调用自定义存储方法的Activity里增加外部存储动态权限获取的代码:
//需要动态授外部存储权限。模拟器不弹动态权限授权对话框,晚点看下什么问题吧
int REQUEST_EXTERNAL_STORAGE = 1;
String[] PERMISSIONS_STORAGE = {"android.permission.READ_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE" };
ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
【Android报错】FileNotFoundException open failed:文件路径 EPERM (Operation not permitted)外部存储至根目录报错,Android外部存储权限动态获取问题的更多相关文章
- 巨坑npm run dev 报错 终于找到正确答案 Error: EPERM: operation not permitted, open '/data/public/build/css/add.p
Windows10环境 npm run dev 报错 终于找到正确答案 Error: EPERM: operation not permitted, open '/data/public/build ...
- npm -v 报错:Error: EPERM: operation not permitted, mkdir 'C:\soft\nodejs'
npm -v 报错:Error: EPERM: operation not permitted, mkdir 'C:\soft\nodejs' 起因:原本安装node在C盘soft文件夹下,按node ...
- npm install 报错(npm ERR! errno -4048,Error: EPERM: operation not permitted,)解决方法
npm ERR! path E:\SouthernPowerGridProject\web_project\AutoOPS\autoops\node_modules\fsevents\node_mod ...
- 建小程序 - 报Error: EPERM : operation not permitted, scandir mac下改变一个目录的访问权限
问题:用微信开发者工具,建一个小程序,报错(见图1): 建小程序 - 报Error: EPERM : operation not permitted, scandir 解决: 1.打开终端 2.cd ...
- root用户删除文件提示:Operation not permitted
root用户删除文件提示:Operation not permitted http://blog.csdn.net/evanbai/article/details/6187578
- android自定义camera以及uri和文件路径之间的转换
相对直接调用系统的camera,这种方法使用得相对还少一些.根据api文档,步骤如下: 定义一个预览类 可以参照<android高薪之路>这本书上面,有这种方法的一种完整实现 而对应的ac ...
- Android与JS进行交互传文件路径
webview+h5这种混合开发最近很火,其中最重要的大概就是java代码和js的交互了,刚接触这东西两天,写写收获. 新建一个assets文件夹,要与res这个文件夹同级,其中存放web项目. 先看 ...
- [Android Pro] root用户删除文件提示:Operation not permitted
reference to : http://blog.csdn.net/evanbai/article/details/6187578 一些文件看上去可能一切正常,但当您尝试删除的时候,居然也会报错, ...
- 解决Linux 下 root用户删除文件提示:Operation not permitted
问题描述 用最高权限rm文件,居然报错Operation not permitted.查看权限也没有问题.可想而知有可能文件被保护了.用命令lsattr检查一下就知道. [root@linux roo ...
- Linux 文件删除 提示 Operation not permitted
Linux 删除 隐藏文件提示 Operation not permitted ? linux 删除 隐藏文件 提示 Operation not permitted 不允许操作? 使用 ls ...
随机推荐
- Install Argo Workflows
Install Argo Workflows Release v3.4.3 · argoproj/argo-workflows (github.com) CLI # Download the bina ...
- Win10下如何添加“中文简体(美式键盘)”输入法
参考Win10下如何添加"中文简体(美式键盘)"输入法 设置>>时间和语言>>语言>>添加语言>>选择要安装的语言>>英 ...
- 下载base64图片
下载 this.downloadFile('二维码',that.img64) downloadFile(fileName, content) { let aLink = document.create ...
- 蓝桥杯训练赛二-1199 问题 E: 哥德巴赫曾猜测
题目描述 德国数学家哥德巴赫曾猜测:任何大于6的偶数都可以分解成两个素数(素数对)的和.但有些偶数可以分解成多种素数对的和,如: 10=3+7,10=5+5,即10可以分解成两种不同的素数对 输入 输 ...
- guide, manual, tutorial之间的区别
总结:一般而言,guide表示用户指南,提供简要的信息:manual表示用户手册,提供较为完整深入的说明:tutorial表示教程,提供详细的说明,侧重于给没有经验的人一步一步进行详细的指导.实际情况 ...
- E: 无法获得锁 /var/lib/apt/lists/lock - open (11: 资源暂时不可用)E: 无法对目录 /var/lib/apt/lists/ 加锁
问题: 解决:https://www.cnblogs.com/long5683/p/11058066.html 使用方法二 可以
- pdf2docx:可将 PDF 转换成 docx 文件的 Python 库
pdf2docx:https://github.com/dothinking/pdf2docx 可将 PDF 转换成 docx 文件的 Python 库. from pdf2docx import p ...
- tapdata问题
聚合节点写两个不同的聚合函数,只需要在关联目标节点的目标字段中添加上分组字段,其他字段不用做关联 聚合节点写两个相同的聚合函数,只需要在关联目标节点的目标字段中左右两边都添加上_id,会输出两条数据, ...
- 2345 ip
121.201.101.43 img1.2345.com121.201.101.43 img2.2345.com121.201.101.43 img3.2345.com121.201.101.43 i ...
- ComPiler200004:Library-Oriented Programming
https://www.aliyun.com/jiaocheng/403982.html 摘要: Creating a simple, encapsulated, maintainable libra ...