FileProviderを使用してMarshmallowとNougatでカメラを開き、onActivityResultメソッドのキャプチャ画像を表示するビットマップを取得しようとしていません。イメージパスから。ここに私のコード私はあなたがカメラやギャラリーのために、このライブラリを使用するために持っていると思うイメージのビットマップをonActivityResultで取得し、画像をイメージビューで設定した後に画像を削除する方法
image_click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent();
}
});
}
static final int REQUEST_TAKE_PHOTO = 1;
private void dispatchTakePictureIntent() {
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
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoURI = FileProvider.getUriForFile(this,
"com.xyz.dummyapp.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
try{
Bitmap bitmap = BitmapFactory.decodeFile(photoURI.getPath());
mImageView.setImageURI(photoURI);
File file=new File(mCurrentPhotoPath);
if (file.exists()){
if (file.delete()){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"Deleted",Toast.LENGTH_SHORT).show();
}
});
}
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.xyz.dummyapp/files/Pictures" />
</paths>
あなたは保存されたインスタンス状態 'Bundle'に、' photoURI'またはそれをビルドするものを保存していません。あなたのプロセスは、カメラアプリがフォアグラウンドにある間にいつでも終了することができ、その場合、あなたのフィールドは「ヌル」になります。 [FileProvider]を 'ACTION_IMAGE_CAPTURE'で適切に使用する方法を示す[サンプルアプリケーションはこちら](https://github.com/commonsguy/cw-omnibus/tree/master/Camera/FileProvider)です。 – CommonsWare