我为 Android 4.3 Jelly Bean 和 Android 8 Oreo 执行此操作:
void startCamera() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
dispatchTakePictureIntent();
}
else
{
boolean focus = getPackageManager().hasSystemFeature( "android.hardware.camera.autofocus");
//displayMsg(context, "In startCamera AutoFocus = " + focus);
dispatchTakePictureIntent4();
}
} catch (IOException e) {
displayMsg(context, "In startCamera " + e.toString());
}
}
public void dispatchTakePictureIntent() throws IOException {
try {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
displayMsg(context, "Error occurred while creating the File " + ex.toString());
return;
}
// Continue only if the File was successfully created
if (photoFile != null) {
//rww OLD here
//Uri photoURI = Uri.fromFile(createImageFile());
Uri photoURI = FileProvider.getUriForFile(TellYourStoryActivity.this,
BuildConfig.APPLICATION_ID + ".provider",
createImageFile());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//added RWW
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
} catch (Exception e) {
displayMsg(context, "dispatchTakePictureIntent " + e.toString());
}
}
public void dispatchTakePictureIntent4() throws IOException {
try {
// MainActivity.java
File f = this.createImageFile4(this);
if (f == null)
{
displayMsg(context, "dispatchTakePictureIntent 444" + " file f is null");
return;
}
String photoPath = "file:" + f.getAbsolutePath();
mCurrentPhotoPath = "file:" + f.getAbsolutePath();
mCurrentAbsolutePhotoPath = f.getAbsolutePath();
//displayMsg(context, "dispatchTakePictureIntent 444 photopath\n" + photoPath);
//Uri photoURI = FileProvider.getUriForFile(this, "com.myapp.fileprovider", f);
Uri photoURI = Uri.fromFile(f);
//displayMsg(context, "dispatchTakePictureIntent 444 uri" + photoURI.toString());
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, NEW_TOPIC_VIA_PHOTO);
} catch (Exception e) {
displayMsg(context, "dispatchTakePictureIntent 444" + e.toString());
}
}
// PhotoHelper.java
public File createImageFile4(Context context) throws IOException {
try {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
return File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, storageDir);
} catch (Exception e) {
displayMsg(context, "createImageFile4 444" + e.toString());
}
return null;
}
public File createImageFile() throws IOException {
// Create an image file name
File image = null;
//displayMsg(context, "In createImageFile ");
try {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM), "Camera");
image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
RWWmCurrentPhotoPath = "content:/" + image.getAbsolutePath();
mCurrentAbsolutePhotoPath = image.getAbsolutePath();
} catch (Exception e) {
displayMsg(context, "In createImageFile " + e.toString());
}
return image;
}