我已经确认相机访问的权限是正确的,但是在较新的操作系统版本(可能是API 25及以上)上,相机无法打开,它只是在调试控制台中给出错误;
W/System.err: java.io.IOException: Permission denied
这就是方法;
public void cameraClicked(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File tempFile = new File(Environment.getExternalStorageDirectory().getPath()+ "/photoTemp.png");
try {
tempFile.createNewFile();
Uri uri = Uri.fromFile(tempFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(takePictureIntent, 2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
它确实可以在Android 7及更低版本上工作。
编辑-下面的代码现在正确地打开了相机,但是一旦拍摄了照片,它就前进到下一个屏幕,但不显示捕获的图像...只是一个黑色的图像。
public void cameraClicked(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String path=this.getExternalCacheDir()+"file.png";
File file=new File(path);
Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",file);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(takePictureIntent, 2);
}
2条答案
按热度按时间cig3rfwq1#
W/系统错误:java.io.IOException:权限被拒绝
发生这种情况是因为您在Android 8/9/10上创建文件到外部存储。
如果您的targetSdk为23或更高,则应动态请求权限。要了解更多信息:在运行时请求权限
要获取文件路径,您可以使用Context.getExternalFilesDir()/Context.getExternalCacheDir(),例如,字符串路径=Context.getExternalCacheDir()+“文件.文本”; File file=new如果文件路径为“Android/data/app package/file name”,则不需要权限
与Android文档中一样,您需要写入外部存储,必须在清单文件中请求WRITE_EXTERNAL_STORAGE权限:
如果您使用API 23(Marshmallow)和更高版本,则需要在运行时请求权限,因为这是一个危险的权限。
参考源链接
参考文献
make file to external
编辑答案
kx7yvsdv2#
我以前也遇到过这个问题,似乎在
AndroidManifest.xml
文件下的Application
标记中添加以下内容可以解决这个问题: