Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
FileNotFoundException打开失败:Android Emulator上的EACCES(权限被拒绝)_Android - Fatal编程技术网

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

FileNotFoundException打开失败:Android Emulator上的EACCES(权限被拒绝),android,Android,以下是活动出错的代码: 错误发生在第outputStream=newfileoutputstream(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);

以下是活动出错的代码: 错误发生在第outputStream=newfileoutputstream(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)
W/libc:无法将属性“qemu.gles”设置为“1”:连接失败;errno=13(权限被拒绝)
W/System.err:java.io.FileNotFoundException:/storage/emulated/0/Movies/sample.jpg:open failed:EACCES(权限被拒绝)
在libcore.io.IoBridge.open中(IoBridge.java:496)
位于java.io.FileOutputStream。(FileOutputStream.java:235)
以上是我得到的错误。我正在android emulator上运行它Pixel XL API 29 在emulator上保存文件是否需要一些特殊权限。

As

Android的许可系统是最大的安全隐患之一 因为这些权限是在安装时请求的。一旦 安装后,应用程序将能够访问所有内容 在没有任何用户确认的情况下授予什么应用程序 在得到允许的情况下做

安卓6.0棉花糖对 权限模型添加了运行时权限,新的 替换现有安装时权限的权限模型 当您以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);
            }
        }
将此项添加到Menifest中:

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

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

舱单:

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

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

嘿!验证您是否正在清单文件中添加权限,如果您试图保存文件,则应使用fileprovider
我正在android emulator Pixel XL API 29上运行它是否需要一些特殊权限才能在emulator上保存文件。
是的,android 10设备或emulator还需要一些特殊权限。没人提过。
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();