java.io.FileNotFoundException: open failed: EACCES (Permission denied)
2016-04-17 09:54
513 查看
最近在项目中上传文件的时候出现该异常。异常的提示信息非常容易理解——你没有权限或者文件不存在。在这里讲述我碰到的一个奇葩问题的解决办法,给大家分享一下。
1.确保manifest中添加的该权限
2.布局文件仅仅用于显示对外置sd卡文件的访问权限
3.Java代码
测试结果:
![](http://img.blog.csdn.net/20160417095300519)
虽然在manifest中声明了write权限,但是还是没有写的权限。
下面引用stackoverflow的一段话作为结尾吧:
引用地址:http://stackoverflow.com/questions/5694933/find-an-external-sd-card-location/24336742
访问外置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
相关文章推荐
- java之FileOutputStream
- Spring mvc学习笔记-学习思路
- java.lang.OutOfMemoryError异常解决方法
- JAVA设计模式初探之桥接模式
- java之args[]接收数据并显示
- spring框架整合ibatis的项目实例代码
- Java 异常处理的误区和经验总结
- 20145311实验二 "Java面向对象程序设计"
- java集合-LinkedList
- spring中如何定义定时任务
- java后台返回一个类指定的几个属性的json包装集
- 快速掌握Maven基本开发
- eclipse配置问题汇总
- 27.JAVA编程思想——违例的限制
- 26.JAVA编程思想——创建自己的违例
- 25.JAVA编程思想——标准Java违例
- java内部比较器、外部比较器实现
- 24.JAVA编程思想——违例差错控制
- 23.JAVA编程思想——实用工具
- 22.JAVA编程思想——排序和搜索