Android camera and photo picker intent

2020-06-18 03:35发布

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.

3条回答
太酷不给撩
2楼-- · 2020-06-18 03:40

After almost half of a year, I found a solution. Actually, I found a library which works with every phone and every android version. If still someone needs a solution for this, here is an answer.

EasyImage did the job!

查看更多
看我几分像从前
3楼-- · 2020-06-18 03:41

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);
查看更多
beautiful°
4楼-- · 2020-06-18 03:58
public String getPathFromURI(Uri contentUri) {
    String res = null;
    String[] proj = {MediaStore.Images.Media.DATA};
    Cursor cursor = getActivity().getContentResolver().query(contentUri, proj, "", null, "");
    if (cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    if (resultCode == getActivity().RESULT_OK) {
        if (requestCode == 200) {
            Uri selectedImageUri = imageReturnedIntent.getData();
            if (null != selectedImageUri) {
                String path = getPathFromURI(selectedImageUri);
                DrawerAdapter.imageViewPP.setImageURI(selectedImageUri);
            }
        }else if(requestCode==0){
            Uri selectedImageUri = imageReturnedIntent.getData();
            if (null != selectedImageUri) {
                String path = getPathFromURI(selectedImageUri);
                //DrawerAdapter.imageViewPP.setImageURI(selectedImageUri);
            }
        }
    }
}

    public String getPathFromURI(Uri contentUri) {
    String res = null;
    String[] proj = {MediaStore.Images.Media.DATA};
    Cursor cursor = getActivity().getContentResolver().query(contentUri, proj, "", null, "");
    if (cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}

Intent intent1 = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 200);

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
查看更多
登录 后发表回答