java.io.FileNotFoundException: open failed: EACCES (Permission denied)

最近在项目中上传文件的时候出现该异常。异常的提示信息非常容易理解——你没有权限或者文件不存在。在这里讲述我碰到的一个奇葩问题的解决办法,给大家分享一下。

访问外置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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值