gpt4 book ai didi

android - FileNotFoundException 打开失败 : EACCES (Permission denied) on Android Emulator

转载 作者:行者123 更新时间:2023-12-04 14:09:24 25 4
gpt4 key购买 nike

以下是 Activity 出错的代码:
错误发生在行 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 模拟器上运行它 像素 XL API 29
在模拟器上保存文件是否需要一些特殊权限。

最佳答案

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

<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(也许,我实际上忘记了)您必须请求用户授予权限。这就是为什么你必须添加上面的源代码。

关于android - FileNotFoundException 打开失败 : EACCES (Permission denied) on Android Emulator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65630710/

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