0

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

    }

}

2 Answers2

0

I see a few things to fix, actually. #1 is a small one, #2 is the real issue I think you're seeing.

  1. This is a small thing, but your onActivityResult(...) implementation should get cleaned up. As it stands, it's possible for super.onActivityResult(...) to be called multiple times in a single call to your onActivityResult(...). If I were you, I'd use a switch that checks the request code, then use an if/else inside each case of the switch for checking the result code.

  2. This is the big one. Some Android devices (Samsung is the biggest offender, in my experience) refuse to store captured images where you ask them to. As a general rule, first, always check the URI you specify in your MediaStore.ACTION_IMAGE_CAPTURE Intent for the image data. If it's not there, then you can check for it at the data URI of the returned Intent. As a last resort, you can Google around for wherever that specific device or camera app might be storing captured images and make sure your code checks that location.

mtrewartha
  • 761
  • 7
  • 18
  • How can I check the URI returned from image captured in data ? – Eman Alaa Eldin Jun 17 '18 at 23:08
  • Are you first checking the URI you specified when you sent the system the `MediaStore.ACTION_IMAGE_CAPTURE` intent (the one that ends in "propic.jpg")? It will be there most of the time. If it's not there, then you'll need to Google around for where that specific device/camera app stores pictures. – mtrewartha Jun 26 '18 at 15:02
0

You should override onActivityResult in host Activity of the fragment

Seddik Fredj
  • 123
  • 2
  • 13