java.io.FileNotFoundException: open failed: EACCES (Permission denied)
来源:互联网 发布:随机化算法 编辑:程序博客网 时间:2024/06/05 04:25
最近在项目中上传文件的时候出现该异常。异常的提示信息非常容易理解——你没有权限或者文件不存在。在这里讲述我碰到的一个奇葩问题的解决办法,给大家分享一下。
访问外置sd卡文件
确切的讲是文件保存的“非”默认位置
1.确保manifest中添加的该权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2.布局文件仅仅用于显示对外置sd卡文件的访问权限
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_0" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:drawablePadding="8dp" android:paddingRight="8dp" android:textSize="16sp" /> <TextView android:id="@+id/tv_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:drawablePadding="8dp" android:paddingRight="8dp" android:textSize="16sp" /></LinearLayout>
3.Java代码
public class MainActivity extends Activity { TextView tv0 = null; TextView tv1 = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv0 = (TextView) findViewById(R.id.tv_0); tv1 = (TextView) findViewById(R.id.tv_1); //TODO 这里修改为你的“外置”sd卡上的一个文件的路径 String imagePath = "/storage/sdcard1/DCIM/春节-2000/DSC_7790.jpg"; RandomAccessFile accFile0 = null; try { accFile0 = new RandomAccessFile(imagePath, "r"); tv0.setText("读没有问题"); } catch (Exception e) { e.printStackTrace(); tv0.setText("读有问题"); } finally { try { if (accFile0 != null) { accFile0.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } RandomAccessFile accFile1 = null; try { accFile1 = new RandomAccessFile(imagePath, "rw"); tv1.setText("写没有问题"); } catch (Exception e) { e.printStackTrace(); tv1.setText("写有问题"); } finally { try { if (accFile1 != null) { accFile1.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
测试结果:
虽然在manifest中声明了write权限,但是还是没有写的权限。
下面引用stackoverflow的一段话作为结尾吧:
.keep in mind: until Android 4.4, the official Android platform has not supported SD cards at all except for two special cases: the old school storage layout where external storage is an SD card (which is still supported by the platform today), and a small feature added to Android 3.0 where it would scan additional SD cards and add them to the media provider and give apps read-only access to their files (which is also still supported in the platform today).Android 4.4 is the first release of the platform that has actually allowed applications to use SD cards for storage. Any access to them prior to that was through private, unsupported APIs. We now have a quite rich API in the platform that allows applications to make use of SD cards in a supported way, in better ways than they have been able to before: they can make free use of their app-specific storage area without requiring any permissions in the app, and can access any other files on the SD card as long as they go through the file picker, again without needing any special permissions.
引用地址:http://stackoverflow.com/questions/5694933/find-an-external-sd-card-location/24336742
0 0
- Android java.io.FileNotFoundException: open failed: EACCES (Permission denied)
- [Android]java.io.FileNotFoundException: open failed: EACCES (Permission denied)
- java.io.FileNotFoundException:open failed: EACCES (Permission denied)
- Android开发java.io.FileNotFoundException:open failed: EACCES(Permission denied)
- java.io.FileNotFoundException: open failed: EACCES (Permission denied)
- java.io.FileNotFoundException: /mnt/sdcard/backup.xml: open failed: EACCES (Permission denied)错误解决
- Android java.io.FileNotFoundException: open failed: EACCES (Permission denied) 问题未解决!!
- android 写sd卡权限问题 java.io.FileNotFoundException:open failed: EACCES (Permission denied)
- 读取设备节点/sys/class/XX :Android java.io.FileNotFoundException: open failed: EACCES (Permission denied)
- 在写音乐播放器的时候遇到java.io.FileNotFoundException: sdcard/music.mp3: open failed: EACCES (Permission denied)
- Android程序报错:Anroid 6.0 权限问题java.io.FileNotFoundException: ……:open failed: EACCES (Permission denied)
- java.io.IOException: open failed: EACCES (Permission denied)问题解决
- 关于 java.io.IOException: open failed: EACCES (Permission denied)
- java.io.IOException: open failed: EACCES (Permission denied)问题解决
- java.io.IOException: open failed: EACCES (Permission denied)问题解决
- java.io.IOException: open failed: EACCES (Permission denied) 问题解决
- 53 java.io.IOException: open failed: EACCES (Permission denied)问题解决
- java.io.IOException: open failed: EACCES (Permission denied)问题
- 技术学习路线
- 【BZOJ1015】星球大战 并查集
- Stack Queue Vector ArrayList
- 无刷新 减少服务器负担 jQuery remove方法
- 第七届蓝桥杯C/C++B组省赛部分题目
- java.io.FileNotFoundException: open failed: EACCES (Permission denied)
- JSON之Jackson(二)
- 每天学习一点新东西
- 设计模式系列总结之策略模式
- java之FileOutputStream
- PHP取整,四舍五入取整、向上取整、向下取整、小数截取。
- 给人类支三招对付AlphaGo
- 腾讯实习生面试2016两道面试题目?(知乎)
- 对一致性Hash算法,Java代码实现的深入研究