I am trying to create Floating action button to take photo and show it in image view in fragment . I managed to do floating that take picture from gallery and show it in my fragment . the problem is : the floating action button that take picture from camera doesn't send it to the fragment like the floating action button of gallery capture. this is my code for both :
private final static int REQUEST_REQUIRED_PERMISSION = 0x01;
private final static int REQUEST_PICK_IMAGE = 0x02;
private final static int REQUEST_IMAGE_CAPTURE=102;
Uri uri;
private FloatingActionButton mFabPickImage;
private Button buckyButton;
private FloatingActionButton btn_fab;
ImageView src_img;
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setCapture();
setupViews();
}
setCapture() is a function for camera floating button and setView() is a function for gallery floating button
private void setupViews() {
mFabPickImage = findViewById(R.id.fab_pick_image);
if (mFabPickImage != null) {
mFabPickImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent;
if (Build.VERSION.SDK_INT >= 19) {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
} else {
intent = new Intent(Intent.ACTION_GET_CONTENT);
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
ActivityLauncher.launchActivityForResult(MainActivity.this,
Intent.createChooser(intent, getString(R.string.app_name)),
REQUEST_PICK_IMAGE);
}
});
}
}
private void setCapture(){
btn_fab=(FloatingActionButton)findViewById(R.id.fab);
btn_fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String root =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()+"propic.jpg";
uri=Uri.parse(root);
startActivityForResult(i,REQUEST_IMAGE_CAPTURE);
}
});
}
this is my onActivity to take each image and send it to segmentImage() function that makes segmentation and send output to fragment
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Logger.debug("requestCode = %d, resultCode = %d, data = %s",
requestCode,
resultCode,
data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Uri ImageCaptureUri=data.getData();
Logger.debug("capture: %s", ImageCaptureUri);
if (ImageCaptureUri != null) {
if (Build.VERSION.SDK_INT >= 19) {
final int takeFlags = data.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION;
getContentResolver()
.takePersistableUriPermission(ImageCaptureUri, takeFlags);
}
segmentImage2(ImageCaptureUri);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
Logger.debug("requestCode = %d, resultCode = %d, data = %s",
requestCode,
resultCode,
data);
if (requestCode == REQUEST_PICK_IMAGE && resultCode == RESULT_OK) {
Uri pickedImageUri = data.getData();
Logger.debug("picked: %s", pickedImageUri);
if (pickedImageUri != null) {
if (Build.VERSION.SDK_INT >= 19) {
final int takeFlags = data.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION;
getContentResolver()
.takePersistableUriPermission(pickedImageUri, takeFlags);
}
segmentImage(pickedImageUri);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
private void segmentImage(Uri pickedImageUri ) {
Fragment fragment = findFragment(R.id.fragment_segment_bitmaps);
if (fragment instanceof SegmentBitmapsFragment) {
((SegmentBitmapsFragment)fragment).segmentBitmap(pickedImageUri);//important note
}
}
private void segmentImage2(Uri ImageCaptureUri ) {
Fragment fragment = findFragment(R.id.fragment_segment_bitmaps);
if (fragment instanceof SegmentBitmapsFragment) {
((SegmentBitmapsFragment)fragment).segmentBitmap(ImageCaptureUri);//important note
}
}