相机不会返回OnActivityResult

时间:2017-07-10 13:37:03

标签: android camera

嗨首先我知道这个问题已经被问到但是我被困在它上面超过一天我想用相机意图拍照并将其保存在ExternalStoragePublicDirectory手机上但是拍完照片之后它不会返回OnActivityResult,也不会保存图片。它在android 6和7中不起作用: 任何帮助都会被暗示!!

Uri imageUri = setImageUri();
Intent intent1 = new 
Intent(MediaStore.ACTION_IMAGE_CAPTURE);

intent1.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);

intent1.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

List<ResolveInfo> resInfoList = 
MedicalActivity.this.getPackageManager().queryIntentActivities(intent1, 
PackageManager.MATCH_DEFAULT_ONLY);

for (ResolveInfo resolveInfo : resInfoList) {
String packageName =resolveInfo.activityInfo.packageName; 


MainActivity.this.grantUriPermission(packageName, imageUri, 
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | 
Intent.FLAG_GRANT_READ_URI_PERMISSION);
}

startActivityForResult(intent1, CAPTURE_IMAGE);

设置图片Uri:

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {

        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "myappimages");
if (!mediaStorageDir.exists()) {mediaStorageDir.mkdir();}
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File file;
        file = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");


        Uri imgUri = FileProvider.getUriForFile(MedicalActivity.this,
                BuildConfig.APPLICATION_ID + ".provider",
                file);

        return imgUri;

    }

文件提供者路径:

<external-path name="myapp_images_path" path="Pictures/myappimages"/>

1 个答案:

答案 0 :(得分:0)

从android版本大于6且等于6你需要请求存储和摄像头的许可 例如,请检查此链接https://androidkennel.org/android-camera-access-tutorial/https://developer.android.com/guide/topics/permissions/requesting.html