FileNotFoundException 打开失败:Android 模拟器上的 EACCES(权限被拒绝)

时间:2021-01-08 14:19:11

标签: android

以下是活动出错的代码: 错误发生在行 outputStream = new FileOutputStream(file);

public class MainActivity extends AppCompatActivity {

    public void layoutToJPG(View v) {
        View screen = v.getRootView();
// or I've also tried: View screen = new View(this);
        screen.setDrawingCacheEnabled(true);
        Bitmap b = screen.getDrawingCache();
        Log.d("bitmap", b.toString());
        File path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_MOVIES);
        File file = new File(path, "/sample.jpg");
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        b.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        Toast.makeText(getApplicationContext(), "Saved to Gallery.", Toast.LENGTH_SHORT).show();
        try {
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

错误:

 W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Movies/sample.jpg: open failed: EACCES (Permission denied)
        at libcore.io.IoBridge.open(IoBridge.java:496)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:235)

以上是我得到的错误。我在 android 模拟器 Pixel XL API 29 上运行它 在模拟器上保存文件是否需要一些特殊权限。

2 个答案:

答案 0 :(得分:2)

您必须添加权限。并且,要求用户接受许可。

清单:

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

请求权限:

您可以使用 library 来请求许可

Dexter.withContext(this)
.withPermission(Manifest.permission.CAMERA)
.withListener(new PermissionListener() {
    @Override public void onPermissionGranted(PermissionGrantedResponse response) {/* ... */}
    @Override public void onPermissionDenied(PermissionDeniedResponse response) {/* ... */}
    @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
}).check();

超过 API 级别 23(也许,我实际上忘记了)您必须请求用户授予权限。这就是为什么你必须添加上面的源代码。

答案 1 :(得分:1)

作为。

<块引用>

Android 的权限系统是最大的安全问题之一 因为在安装时要求这些权限。一次 安装后,该应用程序将能够访问所有内容 在没有任何用户确认的情况下授予究竟是什么应用程序 经许可即可。

Android 6.0 Marshmallow 引入了对 添加了运行时权限的权限模型,一个新的 替换现有安装时权限的权限模型 当您以 API 23 为目标并且应用程序在 Android 6.0+ 上运行时的模型 设备

试试这个代码。

if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        1);
            }
        }

清单中添加:

<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    tools:ignore="ScopedStorage" />