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代码:



    
    

    
        
            
                

                
            
        
    

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的存储权限,开启权限后,问题解决。

转载于:https://www.cnblogs.com/iZander/p/6042387.html

你可能感兴趣的:(Android学习问题记录之open failed EACCES (Permission denied))