gpt4 book ai didi

android-studio - Android 9.+ API 29 :/storage/emulated/0/Pictures/myPic. png 打开失败:EACCES(权限被拒绝)

转载 作者:行者123 更新时间:2023-12-03 17:09:20 25 4
gpt4 key购买 nike

我正在使用 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 打开失败:EACCES(权限被拒绝)。我已经尝试了所有在线提供的建议,例如:

  • 在 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" />

  • 在 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
    );
    }
    }

  • 然后我在 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();
    }

    ...

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

  • 但是,没有任何建议有效!

    代码抛出了一个异常

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

    当它到达线
    outStream = new FileOutputStream(file);


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

    最佳答案

    添加 android:requestLegacyExternalStorage="true"到 Android Manifest 为我工作的 Android 29

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

    关于android-studio - Android 9.+ API 29 :/storage/emulated/0/Pictures/myPic. png 打开失败:EACCES(权限被拒绝),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56992682/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com