Android 9.+ API 29:/storage/emulated/0/Pictures/myPic.png 打开失败:EACCES(权限被拒绝) [英] Android 9.+ API 29: /storage/emulated/0/Pictures/myPic.png open failed: EACCES (Permission Denied)

查看:104
本文介绍了Android 9.+ API 29:/storage/emulated/0/Pictures/myPic.png 打开失败:EACCES(权限被拒绝)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Android Virtual Device (AVD) Nexus 7 (2012) API 29 在 Android Studio 3.4.1 上尝试一个非常基本的相机应用程序的源代码.我在模拟器中用相机拍了一张照片并试图保存它作为/storage/emulated/0/Pictures/myPic.png,但有一个异常/storage/emulated/0/Pictures/myPic.png open failed: EACCES (Permission Denied).我已经尝试了所有在线提供的建议,例如:

I am trying the source code of a very basic camera app on Android Studio 3.4.1 with Android Virtual Device (AVD) Nexus 7 (2012) API 29. I took a picture with the camera in the emulator and tried to save it as /storage/emulated/0/Pictures/myPic.png, but got an exception of /storage/emulated/0/Pictures/myPic.png open failed: EACCES (Permission Denied). I have tried all advice available online, for instance:

  1. 在 AndroidManifest.xml 中,我添加了:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  1. 在 MainActivity.java 中,我添加了以下运行时权限检查和请求代码:

public class MainActivity extends Activity {

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    public static void verifyStoragePermissions(Activity activity) {
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        boolean checkPermission = (permission == PackageManager.PERMISSION_GRANTED);

        Toast toastPermission = Toast.makeText(activity,
                "Is permission granted? " + checkPermission,
                Toast.LENGTH_SHORT);
        LinearLayout toastLayoutPermission = (LinearLayout) toastPermission.getView();
        TextView toastTVPermission = (TextView) toastLayoutPermission.getChildAt(0);
        toastTVPermission.setTextSize(30);
        toastPermission.show();


        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }

  1. 然后我在 onCreate 和我想保存图片之前检查并请求许可,如下所示:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        verifyStoragePermissions(this);


......


private void saveImageToFile(File file)
    {
        if (mCameraBitmap != null)
        {
            FileOutputStream outStream = null;

            verifyStoragePermissions(this);

            try
            {
                outStream = new FileOutputStream(file);

                ...

            }
            catch (Exception e)
            {
                Toast toastException = Toast.makeText(MainActivity.this,
                        "Exception: " + e.getMessage(),
                        Toast.LENGTH_SHORT);
                LinearLayout toastLayoutException = (LinearLayout) toastException.getView();
                TextView toastTVException = (TextView) toastLayoutException.getChildAt(0);
                toastTVException.setTextSize(30);
                toastException.show();
            }

...

  1. 我还在模拟器中手动检查了存储权限,以确保允许访问存储.

然而,没有一个建议有效!

However, NONE of the advice works!

代码抛出异常

/storage/emulated/0/Pictures/myPic.png 打开失败:EACCES(权限被拒绝)

/storage/emulated/0/Pictures/myPic.png open failed: EACCES (Permission Denied)

当它到达

outStream = new FileOutputStream(file);

有什么想法可以进一步解决这个问题吗?非常感谢.

Any idea to further solve this? Thanks a lot.

推荐答案

android:requestLegacyExternalStorage="true" 添加到 Android Manifest 对我来说适用于 Android 29

Adding android:requestLegacyExternalStorage="true" to the Android Manifest worked for me with Android 29

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:requestLegacyExternalStorage="true" ...

这篇关于Android 9.+ API 29:/storage/emulated/0/Pictures/myPic.png 打开失败:EACCES(权限被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆