Try this way, hope it helps you solve your problem
Please follow these steps and this is the best and easiest way to select an image from the gallery and from the camera,
Step 1. Declaring global variables
public static final int rcCC = 33; boolean isCC = false;
Step 2. Copy these methods and paste the code
void picPhoto() { String str[] = new String[] { "Camera", "Gallery" }; new AlertDialog.Builder(getActivity()).setItems(str, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { performImgPicAction(which); } }).show(); } void performImgPicAction(int which) { Intent in; if (which == 1) { in = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); } else { in = new Intent(); in.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); } startActivityForResult( Intent.createChooser(in, "Select profile picture"), which); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); try { if (rcCC == requestCode) { getActivity(); if (resultCode == Activity.RESULT_OK) { isCC = true; } } else { getActivity(); if (resultCode == Activity.RESULT_OK) { BitmapFactory.Options option = new BitmapFactory.Options(); option.inDither = false; option.inPurgeable = true; option.inInputShareable = true; option.inTempStorage = new byte[32 * 1024]; option.inPreferredConfig = Bitmap.Config.RGB_565; if (Build.VERSION.SDK_INT < 19) { Uri selectedImageURI = data.getData(); uImage = BitmapFactory.decodeFile(Common.getPath( selectedImageURI, getActivity()), option); } else { ParcelFileDescriptor pfd; try { pfd = context .getContentResolver() .openFileDescriptor(data.getData(), "r"); FileDescriptor fileDescriptor = pfd .getFileDescriptor(); uImage = BitmapFactory.decodeFileDescriptor( fileDescriptor, null, option); pfd.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } try { ivProfile_image.setImageBitmap(uImage); } catch (Exception e) { e.printStackTrace(); } } } } catch (Exception e) { e.printStackTrace(); } }
Step 3. Call the method in which you want to select pic and show the dialog
picPhoto();
Step 4
Make the user permission in the manifest file,
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CAMERA" />
thanks
Saveen
source share