Android camera and photo picker intent

Igor Janković picture Igor Janković · Aug 16, 2016 · Viewed 26.6k times · Source

I'm building the android app which one of the functionality will be taking the picture from the gallery or from the camera. I have a problem making it to work with every device. I can't find the proper solution for every android version and every device. I feel like I've searched all over the internet and tried every code that I found but without success to make it work for everything mentioned. I've tried implementing the code from the official android documentation but same problem. Picking from the gallery seem's to be working, but camera doesn't work well at all. Could someone please give me the tip, link or the code how to do it? I literally lost my nerves trying to make this. I'm pretty new in android...

I need both intents, for gallery and camera.

Answer

Muazzam A. picture Muazzam A. · Aug 16, 2016

You can do some thing like this,

private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_GALLARY = 2;


 Uri outPutfileUri;

//For RunTime Permision like in Marshmallow os

private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    }; 




//Define Button in the Xml file and get them
Button galleryButton= (Button)findViewById(R.id.gallerybutton);
Button cameraButton= (Button)findViewById(R.id.camerabutton);

//Listener's on the button 
galleryButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK,                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            // Start the Intent
            startActivityForResult(galleryIntent, PICK_FROM_GALLARY);
        }
    });



    cameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Camera permission required for Marshmallow version

                // permission has been granted, continue as usual
                Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                File file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg");
                outPutfileUri = Uri.fromFile(file);
                captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
                startActivityForResult(captureIntent, PICK_FROM_CAMERA);
            }
        }
    });

Then on ActivityResult you will get the image

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

        switch (requestCode) {
            case PICK_FROM_CAMERA:
                if (resultCode == Activity.RESULT_OK) {
                    //pic coming from camera
                    Bitmap bitmap=null;
                    try {
                    bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), outPutfileUri);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                }
            break;

            case PICK_FROM_GALLARY:

                if (resultCode == Activity.RESULT_OK) {
                    //pick image from gallery
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };

                    // Get the cursor
                    Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    // Move to first row
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String imgDecodableString = cursor.getString(columnIndex);
                    cursor.close();
                    bitmap = BitmapFactory.decodeFile(imgDecodableString);




                }
            break;
        }
    }

// For Marshmallow device

cameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Camera permission required for Marshmallow version
                if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
                    // Callback onRequestPermissionsResult interceptadona Activity MainActivity
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, PICK_FROM_CAMERA);
                } else {
                    // permission has been granted, continue as usual
                    Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(captureIntent, PICK_FROM_CAMERA);
                }
            }
        }); 


//Gallery storage permission required for Marshmallow version
    public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }

//For Android 7.0 you can do something like that

ContentValues values = new ContentValues(1);
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
        outPutfileUri = getActivity().getContentResolver()
                .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
        | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
        startActivityForResult(captureIntent, PICK_FROM_CAMERA);