不同SDK的相机权限
camera permission for different SDK
我正在尝试创建一个应用程序来捕获一些图像然后通过电子邮件发送。
我的问题是:
我是否需要为每个 API 创建不同的代码来访问相机和外部存储?!
因为我已经创建了这段代码并且在我的 phone (API 22) 上运行良好:
public void startCameraIntent(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takenPicture = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "image.jpg");
Uri tempURI = Uri.fromFile(takenPicture);
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
startActivityForResult(intent, CAMERA_INTENT);
}
在这段代码中,我试图将捕获的图像保存在 takenPicture 文件中,并保存在设备中的特定位置。
当我尝试在另一台具有更高 API(23 或 24)的设备上 运行 此代码时,我得到 Permission Denied for CAMERA
,尽管我已经在 [=32] 中添加了权限=] 文件:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera.any" android:required="true"/>
谁能告诉我我还在学习,我真的很想知道如何让这段代码适用于任何设备。
我第一次创建项目的时候把Lollipop设为我最低的android系统
在 Marshmallow 下,所有权限均在安装时隐式授予。但是在 API 23 及以上需要在运行时明确询问用户所需的权限。
这是来自官方的示例documentation。
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.CAMERA)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
arrayOf(Manifest.permission.CAMERA),
MY_PERMISSIONS_REQUEST_CAMERA)
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
我正在尝试创建一个应用程序来捕获一些图像然后通过电子邮件发送。
我的问题是:
我是否需要为每个 API 创建不同的代码来访问相机和外部存储?!
因为我已经创建了这段代码并且在我的 phone (API 22) 上运行良好:
public void startCameraIntent(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takenPicture = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "image.jpg");
Uri tempURI = Uri.fromFile(takenPicture);
intent.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
startActivityForResult(intent, CAMERA_INTENT);
}
在这段代码中,我试图将捕获的图像保存在 takenPicture 文件中,并保存在设备中的特定位置。
当我尝试在另一台具有更高 API(23 或 24)的设备上 运行 此代码时,我得到 Permission Denied for CAMERA
,尽管我已经在 [=32] 中添加了权限=] 文件:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera.any" android:required="true"/>
谁能告诉我我还在学习,我真的很想知道如何让这段代码适用于任何设备。
我第一次创建项目的时候把Lollipop设为我最低的android系统
在 Marshmallow 下,所有权限均在安装时隐式授予。但是在 API 23 及以上需要在运行时明确询问用户所需的权限。
这是来自官方的示例documentation。
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.CAMERA)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
arrayOf(Manifest.permission.CAMERA),
MY_PERMISSIONS_REQUEST_CAMERA)
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}