相机不返回OnActivityResult [英] Camera does not return to OnActivityResult
问题描述
首先,我知道已经有人问过这个问题,但是我坚持不止一天,我想用相机拍摄一张照片并将其保存在 ExternalStoragePublicDirectory
手机,但拍照后不会返回 OnActivityResult
,也不会保存图片。它在android 6和7中不起作用:
任何帮助都将得到!!
Hi first of all i know this question already has been asked but i am stuck on it more than one day i want to take a picture with camera intent and save it on ExternalStoragePublicDirectory
of phone but after taking the picture it doesn't return to OnActivityResult
nor the picture is saved. it's not working in android 6 and 7:
any help would be appericiated!!
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"/>
推荐答案
从android版本大于6等于6需要请求存储和照相机
的许可,例如,检查此链接 https:/ /androidkennel.org/android-camera-access-tutorial/ 和 https://developer.android.com/guide/topics/permissions/requesting.html
from android version greater than 6 and equal to 6 you need to request for permission of storage and camera for example check this link https://androidkennel.org/android-camera-access-tutorial/ and https://developer.android.com/guide/topics/permissions/requesting.html
这篇关于相机不返回OnActivityResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!