我的代码有问题 我创建了一个包含2个项目的警报对话框 其中一个用于从画廊导入图像,没有问题 另一个是用相机拍照 我不知道究竟是什么问题 这是我的活动代码:
AlertDialog.Builder builder = new AlertDialog.Builder(NewAdActivity.this);
builder.setAdapter(new ArrayAdapter<String>(NewAdActivity.this, R.layout.row, R.id.mytext, list), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (i == 0) {//gallery
Intent gallery_intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(gallery_intent, "لطفا یک عکس را انتخاب کنید"), 2);
} else {//camera
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),"file"+String.valueOf(System.currentTimeMillis()+".jpg"));
Uri uri = Uri.fromFile(file);
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
camera_intent.putExtra("return-data",true);
startActivityForResult(camera_intent,1);
}
我还添加了权限 这是日志猫:
致命的例外:主要 处理:nr.nima.sheypor,PID:17556 java.lang.SecurityException:Permission Denial:启动Intent { act = android.media.action.IMAGE_CAPTURE flg = 0x3 cmp = com.sec.android.app.camera / .Camera clip = {text / uri-list U:file:///storage/emulated/0/file1502095189530.jpg}(有额外内容)} 来自ProcessRecord {1839049 17556:nr.nima.sheypor / u0a833}(pid = 17556, uid = 10833)具有撤销权限android.permission.CAMERA 在android.os.Parcel.readException(Parcel.java:1620) 在android.os.Parcel.readException(Parcel.java:1573) 在 android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3130) 在 android.app.Instrumentation.execStartActivity(Instrumentation.java:1541) 在android.app.Activity.startActivityForResult(Activity.java:4284) 在 android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48) 在 android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75) 在android.app.Activity.startActivityForResult(Activity.java:4231) 在 android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856) at nr.nima.sheypor.NewAdActivity $ 6.onClick(NewAdActivity.java:176) 在 android.support.v7.app.AlertController $ AlertParams $ 3.onItemClick(AlertController.java:959) 在android.widget.AdapterView.performItemClick(AdapterView.java:346) 在android.widget.AbsListView.performItemClick(AbsListView.java:1547) 在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3827) 在android.widget.AbsListView $ 3.run(AbsListView.java:5849) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:7223) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:0)
从Android 6.0(API级别23)开始,用户在应用运行时向应用授予权限。
要检查是否授予了权限,请致电:
private Boolean hasPermissionCamera() {
boolean hasPermission = (ContextCompat.checkSelfPermission(CamActivity.this,
Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED);
return hasPermission;
}
您可以使用您要求的权限检查更改Manifest.permission.CAMERA的存储权限。
修改的
我整合了您的代码 selectedIdTVOnClick是一个按钮点击 您可以从以前调用的地方呼叫“警报”构建器
试试这个
private Boolean hasPermissionCamera() {
boolean hasPermission = (ContextCompat.checkSelfPermission(StartActivity.this,
Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED);
return hasPermission;
}
AlertDialog mSelectDialog;
public void selectedIdTVOnClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(StartActivity.this);
final String[] LIST_TYPES_ITEMS = Arrays.asList("0", "1").toArray(new String[] {});
builder.setSingleChoiceItems(LIST_TYPES_ITEMS, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {//gallery
Intent gallery_intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(gallery_intent, "لطفا یک عکس را انتخاب کنید"), 2);
} else {//camera
if(hasPermissionCamera()){
startCamera();
}else {
initaskPermission();
}
}
}
});
mSelectDialog = builder.create();
mSelectDialog.show();
}
private static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
private void initaskPermission() {
List<String> listPermissionsNeeded = new ArrayList<>();
if (!hasPermissionCamera()) {
listPermissionsNeeded.add(Manifest.permission.CAMERA);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<>();
// Initialize the map with both permissions
perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
// Fill with actual results from user
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) {
Log.d(TAG, "sms & location services permission granted");
startCamera();
} else {
//Permission denied
// Display a toast or recall initAskPermission
}
}
}
}
}
private void startCamera() {
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),"file"+String.valueOf(System.currentTimeMillis()+".jpg"));
Uri uri = Uri.fromFile(file);
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
camera_intent.putExtra("return-data",true);
startActivityForResult(camera_intent,1);
}