Android Studio java.io.IOException:在Andoid上使用相机时权限被拒绝?

i1icjdpr  于 2022-11-16  发布在  Android
关注(0)|答案(2)|浏览(202)

我已经确认相机访问的权限是正确的,但是在较新的操作系统版本(可能是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);
    }
cig3rfwq

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权限:

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

如果您使用API 23(Marshmallow)和更高版本,则需要在运行时请求权限,因为这是一个危险的权限。

if (ContextCompat.checkSelfPermission(
        CONTEXT, Manifest.permission.REQUESTED_PERMISSION) ==
        PackageManager.PERMISSION_GRANTED) {
    // You can use the API that requires the permission.
    performAction(...);
} else if (shouldShowRequestPermissionRationale(...)) {
    // In an educational UI, explain to the user why your app requires this
    // permission for a specific feature to behave as expected. In this UI,
    // include a "cancel" or "no thanks" button that allows the user to
    // continue using your app without granting the permission.
    showInContextUI(...);
} else {
    // You can directly ask for the permission.
    // The registered ActivityResultCallback gets the result of this request.
    requestPermissionLauncher.launch(
            Manifest.permission.REQUESTED_PERMISSION);
}

参考源链接
参考文献
make file to external

编辑答案

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {        
    switch (requestCode){
        case 0:
            if (resultCode == Activity.RESULT_OK){
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                SaveImage(thumbnail);                    
            }
            break;
    }
}

private static void SaveImage(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().getAbsolutePath();
    File myDir = new File(root + "/saved_images");
    myDir.mkdirs();

    String fname = "Image-"+ Math.random() +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete ();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
kx7yvsdv

kx7yvsdv2#

我以前也遇到过这个问题,似乎在AndroidManifest.xml文件下的Application标记中添加以下内容可以解决这个问题:

<application
    ...
    android:requestLegacyExternalStorage="true">
</application>

相关问题