0

I need to take a photo from my app and display it on an imageView, so now I´m using an intent request:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent, REQUEST_CAMERA);

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CAMERA) {
                bm = (Bitmap) data.getExtras().get("data");
                imageView.setImageBitmap(bm);
            } 
        }
    }

The problem is that this code gets the thumbail of the image, not the image itself in full resollution, and I don´t know how to get it. I´ve searched and I have found this https://developer.android.com/training/camera/photobasics.html , but as I´m starting with this i don´t understand it.

Could somebody help me please?

Javierd98
  • 708
  • 9
  • 27
  • 1
    All is explained in that document under `Save the Full-size Photo`. So what is it that you do not understand? You posted the code for getting a thumbnail. But you could better have posted code for getting a full size image. – greenapps Jul 14 '16 at 13:10
  • 1
    The link you given itself answers the question.. – Sunil Sunny Jul 14 '16 at 13:13
  • getExtras().get("data") gives you the thumbnail, but it may be null. The answer by guarang shows how to access the photo file itself. Note that it may exhaust your memory if used to setImageURI() without caution – Alex Cohn Jul 14 '16 at 13:54

3 Answers3

2

Before you call CameraIntent create a file and uri based on that filepath as shown here.

filename = Environment.getExternalStorageDirectory().getPath() + "/test/testfile.jpg";
imageUri = Uri.fromFile(new File(filename));

// start default camera
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                imageUri);
startActivityForResult (cameraIntent, CAMERA_PIC_REQUEST);

Now, you have the filepath you can use it in onAcityvityResult method as following,

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode != CAMERA_PIC_REQUEST || filename == null)
        return;
    ImageView img = (ImageView) findViewById(R.id.image);
    img.setImageURI(imageUri);
gaurang
  • 2,217
  • 2
  • 22
  • 44
  • `create a file`. Luckily you are not doing that. But you should add code that checks if the directory exists and if not create it. – greenapps Jul 14 '16 at 13:57
  • In fact, this is not working, you can take a photo but altought you choose the photo clicking the Check, the camera app doesn´t open – Javierd98 Jul 14 '16 at 14:12
0

Try out this code, It works for me. Open camera using below code:

Intent intent = new Intent();
            try {
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)&& !Environment.getExternalStorageState().equals(
                        Environment.MEDIA_MOUNTED_READ_ONLY)) {
                    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                    File file = new File(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator+ ".your folder name"
                            + File.separator + "picture").getPath());
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    capturedImageUri = Uri.fromFile(File.createTempFile("your folder name" + new SimpleDateFormat("ddMMyyHHmmss", Locale.US).format(new Date()), ".jpg", file));
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
                    startActivityForResult(intent, Util.REQUEST_CAMERA);
                } else {
                    Toast.show(getActivity(),"Please insert memory card to take pictures and make sure it is write able");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

In onActivityResult retrieve path of captured image:

try {
                    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && !Environment.getExternalStorageState().equals(
                            Environment.MEDIA_MOUNTED_READ_ONLY)) {
                        File file = new File(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator
                                + ".captured_Images"+ File.separator+ "picture").getPath());
                        if (!file.exists()) {
                            file.mkdirs();
                        }

selectedPath1 = capturedImageUri.toString().replace("file://", "");
                            Logg.e("selectedPath1", "OnactivityResult==>>" + selectedPath1);
                            imageLoader.displayImage(capturedImageUri.toString(), imgCarDetails1);
} else {
                        Toast.show(getActivity(), "Please insert memory card to take pictures and make sure it is write able");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

Add this permission in Manifest file:-

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    @SuppressWarnings("deprecation")
    Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
Bhavnik
  • 2,020
  • 14
  • 21
0

None of the answers worked for me, and I couldn´t use the tutorial as when creating the FileProvider, the xml gives me an error. Finally I found this answer which works like a charm. Android Camera Intent: how to get full sized photo?

Community
  • 1
  • 1
Javierd98
  • 708
  • 9
  • 27