Android学习问题记录之open failed EACCES (Permission denied)


1.问题描述

Android调用相机拍照保存,然后读取保存好的照片,在读取照片时出现异常(该异常是因为没有SD卡的读取权限所致):

11-08 11:07:46.421 8539-8539/com.choosepictest D/CROP_PHOTO: /storage/emulated/0/output_image.jpg: open failed: EACCES (Permission denied)

下面是Java代码:

public void btnTakePhoto_onClick(View view)
{
    picture = (ImageView)findViewById(R.id.ivPicture);

    // 创建File对象,用于存储拍照后的图片
    File outputImage = new File(Environment.getExternalStorageDirectory(),"output_image.jpg");

    try
    {
        if(outputImage.exists())
        {
            outputImage.delete();
        }
        outputImage.createNewFile();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    imageUri = Uri.fromFile(outputImage);
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
    startActivityForResult(intent,TAKE_PHOTO);
}

@Override
protected  void onActivityResult(int requestCode,int resultCode,Intent data)
{
    switch (requestCode)
    {
        case TAKE_PHOTO:
            if(resultCode == RESULT_OK)
            {
                Intent intent = new Intent("com.android.camera.action.CROP");
                intent.setDataAndType(imageUri,"image/*");
                intent.putExtra("scale",true);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
                startActivityForResult(intent,CROP_PHOTO);
            }
            break;
        case CROP_PHOTO:
            if(resultCode == RESULT_OK)
            {
                try
                {
                    Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                    picture.setImageBitmap(bitmap);
                }
                catch (FileNotFoundException e)
                {
                    e.printStackTrace();
                    Log.d("CROP_PHOTO",e.getMessage());
                }
            }
            break;
    }
}

因为是权限问题,所以在AndroidManifest.xml中添加了相应权限,但是问题没有解决,下面是AndroidManifest.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.choosepictest">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

2.解决方法

在往上查找了一些解决办法,基本上都是添加权限,但是权限我已经添加,问题还是没有解决,暂时没有找到解决办法。下面是网上的解决方法:

http://blog.csdn.net/zxkevin1989/article/details/7464550/
http://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android
http://stackoverflow.com/questions/26208751/filenotfoundexception-storage-emulated-0-pictures-pic-jpg-open-failed-eacces

注:上述解决办法都是正确的,是我的手机自动禁用了App的存储权限,开启权限后,问题解决。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM