2

When i am starting camera , the data is null. I don't know why.. This is Camera's code:

 ((Button)view.findViewById(R.id.BtnOpenCamera)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                String file = "myimg.jpg";

                File path =  new File(Environment.getExternalStorageDirectory()+"/"+file);
                Uri outputFileUri = FileProvider.getUriForFile(getActivity().getApplicationContext(), BuildConfig.APPLICATION_ID
                        + ".provider", path);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                cameraIntent.putExtra("return-data", true);
                startActivityForResult(cameraIntent, 0);
            }
        });

EDİT ; Added onActivityResult code block

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.e(LoggerID,requestCode+" "+resultCode+" "+(data != null));
}
forpas
  • 160,666
  • 10
  • 38
  • 76
XarzaMora
  • 25
  • 1
  • 6

3 Answers3

0

Check if this helps

((Button)view.findViewById(R.id.BtnOpenCamera)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Starts the Camera
            Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intentCamera, 0);
        }
    });

Then to catch the uri in onActivityResult()

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 0){

        try {
            // 1) Get the bitmap from the taken picture;
            Bitmap bitmap = (Bitmap) data.getExtras().get("data");

            // 2) Converts the bitmap into image String URL;
            Uri uri = ImageManager.getImageUriFromBitmap(getActivity(), bitmap, 50);
            String imgURL = ImageManager.getRealPathFromUri(getActivity(), uri);

            // Use the imgURL the way you need.

        } catch (NullPointerException e){e.printStackTrace();}
    }
}
Soon Santos
  • 2,107
  • 22
  • 43
0

If the camera opens on the fragment, it should also be called in activity

mmfarzaneh
  • 381
  • 1
  • 11
0

The ACTION_IMAGE_CAPTURE intent is fulfilled by whatever camera app is registered to handle it on the specific device. Such apps vary significantly, even if you only look at "official" camera apps that are preinstalled on the device by manufacturer.

Not all these apps, especially on niche devices, fully comply with the official specs. Samsung phones also are known to treat the intent contract requirements broadly.

In your case, the code uses on an undocumented "return-data" extra. I see it mentioned on some Samsung-related forums, but even then there is no evidence that it actually works.

The only reliable reference to data for ACTION_IMAGE_CAPTURE intent states,

The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image.

But in real life, even this is not guaranteed. This means that your app should be prepared to get null data from ACTION_IMAGE_CAPTURE, simply because you do not control the app that brings the image to your onActivityResult(), even if you don't pass the extra EXTRA_OUTPUT.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307