gpt4 book ai didi

java - Android 相机未打开以捕获图像

转载 作者:行者123 更新时间:2023-12-02 09:40:02 27 4
gpt4 key购买 nike

这是相机 Intent

 if(f[which].equals("Camera")) {
try {
String imageFileName="outputImage_" + System.currentTimeMillis()+".png";
File outputImageFile=new File(pictureSaveFolderPath,imageFileName);
if(outputImageFile.exists()){
outputImageFile.delete();
}
outputImageFile.createNewFile();
imageUri=getImageUriByOsVersion(outputImageFile);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent, 1);
} catch (Exception ex) {
ex.printStackTrace();
}
}

Activity 结果

 if (requestCode == 1) {
Uri selectedImage=imageUri;
getApplicationContext().getContentResolver().notifyChange(selectedImage,null);
ContentResolver cr=getApplicationContext().getContentResolver();
try {
thumbnail = MediaStore.Images.Media.getBitmap(
cr, selectedImage);
imageView.setImageBitmap(thumbnail);
common.setBitmap(thumbnail);

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

Uri tempUri = getImageUri(getApplicationContext(), thumbnail);
File finalFile = new File(getRealPathFromURI(tempUri));
}

运行时权限

 private  boolean checkAndRequestPermissions() {
int camera = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
int writePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int readPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
List<String> listPermissionsNeeded = new ArrayList<String>();
if (camera != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.CAMERA);
}
if (writePermission != PackageManager.PERMISSION_GRANTED){
listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}if (readPermission != PackageManager.PERMISSION_GRANTED){
listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}


@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<>();
perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_EXTERNAL_STORAGE,PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE,PackageManager.PERMISSION_GRANTED);
if (grantResults.length > 0) {
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
if (perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
showDialogOK("Sorry, Camera and Storage Permission required for this activity. So please ensure the Camera and Storage permissions are enabled in settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
checkAndRequestPermissions();
break;
} }
});
}
} }
} }
}

//AndroidManifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature
android:name="android.hardware.camera.any"
android:required="true" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />

我正在实现相机、图库和文件管理器 Intent ,该代码几天前就已工作,我不知道发生了什么,它现在无法工作。图库和文件管理器按照我的预期完美工作,但相机 Intent 根本不工作工作。

注意:java.lang.SecurityException:UID 10363没有权限

当我选择相机选项时出现上述错误

最佳答案

您基本上可以在运行时使用请求权限,我也没有像您的风格那样获得权限,但我可以像下面的代码一样在运行时获得权限:

ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.[YourPermission]},
1);


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {

case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// do something
} else {
// do something
}
break;
}

关于java - Android 相机未打开以捕获图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57157595/

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