Прежде всего объявите этот блок в файле манифеста для использования камеры.
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
Затем проверьте разрешения для Android 23 SDK и выше.
if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
}
}
Объявите метод для нового файл изображения.
private File getPictureFile() {
String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String pictureFile = "ZOFTINO_" + timeStamp;
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(pictureFile, ".jpg", storageDir);
pictureFilePath = image.getAbsolutePath();
return image;
}
Затем создайте метод фотосъемки.
private void takePicture() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(cameraIntent, REQUEST_PICTURE_CAPTURE);
File pictureFile = null;
try {
pictureFile = getPictureFile();
} catch (IOException ex) {
Toast.makeText(this,
"Photo file can't be created, please try again",
Toast.LENGTH_SHORT).show();
return;
}
if (pictureFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.zoftino.android.fileprovider",
pictureFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(cameraIntent, REQUEST_PICTURE_CAPTURE);
}
}
}
Затем определите FileProvider в файле манифеста.
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.zoftino.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_list"></meta-data>
</provider>
Определите список путей к файлам для FileProvider в файле xml и сохраните его в папке res / xml.
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="zoftino_pic"
path="Android/data/zoftino.com.camera/files/Pictures" />
</paths>
Наконец вы получаете результат камеры из метода OnActivityResult.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_PICTURE_CAPTURE && resultCode == RESULT_OK) {
File imgFile = new File(pictureFilePath);
if(imgFile.exists()) {
// You will use imgFile.
}
}
}