this question has been asked on this platform but none of them helped me. So this application has a button Click Photo with a callback to onPhotoClicked(). I have mentioned the WRITE_EXTERNAL_STORAGE permission in the Manifest. After clicking the photo the cancel button returns to the activity but the ok button does nothing.
Here is the Code:
public class MainActivity extends AppCompatActivity {
Button emailButton, clickPhotoButton;
private static final String FILE_NAME="image01.jpg";
private static final int CAMERA_PIC_REUEST = 100;
private static final int REQUEST_WRITE_EXTERNAL_STORAGE = 1;
File pictureDir;
Uri fileUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailButton = (Button) findViewById(R.id.email_button);
clickPhotoButton = (Button) findViewById(R.id.click_photo_button);
pictureDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Prakhar");
if(!pictureDir.exists()) {
pictureDir.mkdirs();
}
}
public void onPhotoClicked(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File(pictureDir, FILE_NAME);
fileUri = Uri.fromFile(image);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAMERA_PIC_REUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_PIC_REUEST && resultCode == RESULT_OK) {
ImageView imageView = (ImageView) findViewById(R.id.image_view);
File image = new File(pictureDir, FILE_NAME);
fileUri = Uri.fromFile(image);
imageView.setImageURI(fileUri);
}
}
}