Android Camera And Photo Picker Intent
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
Solution 1:
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!
Solution 2:
You can do some thing like this,
privatestaticfinalint PICK_FROM_CAMERA = 1;
privatestaticfinalint PICK_FROM_GALLARY = 2;
Uri outPutfileUri;
//For RunTime Permision like in Marshmallow os
privatestatic 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(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
IntentgalleryIntent=newIntent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, PICK_FROM_GALLARY);
}
});
cameraButton.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
//Camera permission required for Marshmallow version// permission has been granted, continue as usualIntentcaptureIntent=newIntent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Filefile=newFile(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
publicvoidonActivityResult(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 galleryUriselectedImage= data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursorCursorcursor= getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
intcolumnIndex= cursor.getColumnIndex(filePathColumn[0]);
StringimgDecodableString= cursor.getString(columnIndex);
cursor.close();
bitmap = BitmapFactory.decodeFile(imgDecodableString);
}
break;
}
}
// For Marshmallow device
cameraButton.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
//Camera permission required for Marshmallow versionif (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Callback onRequestPermissionsResult interceptadona Activity MainActivityActivityCompat.requestPermissions(getActivity(), newString[]{Manifest.permission.CAMERA}, PICK_FROM_CAMERA);
} else {
// permission has been granted, continue as usualIntent captureIntent = newIntent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, PICK_FROM_CAMERA);
}
}
});
//Gallery storage permission required for Marshmallow versionpublicstaticvoidverifyStoragePermissions(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 userActivityCompat.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);
Solution 3:
public String getPathFromURI(Uri contentUri) {
Stringres=null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursorcursor= getActivity().getContentResolver().query(contentUri, proj, "", null, "");
if (cursor.moveToFirst()) {
intcolumn_index= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (resultCode == getActivity().RESULT_OK) {
if (requestCode == 200) {
UriselectedImageUri= imageReturnedIntent.getData();
if (null != selectedImageUri) {
Stringpath= getPathFromURI(selectedImageUri);
DrawerAdapter.imageViewPP.setImageURI(selectedImageUri);
}
}elseif(requestCode==0){
UriselectedImageUri= imageReturnedIntent.getData();
if (null != selectedImageUri) {
Stringpath= getPathFromURI(selectedImageUri);
//DrawerAdapter.imageViewPP.setImageURI(selectedImageUri);
}
}
}
}
public String getPathFromURI(Uri contentUri) {
Stringres=null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursorcursor= getActivity().getContentResolver().query(contentUri, proj, "", null, "");
if (cursor.moveToFirst()) {
intcolumn_index= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
Intentintent1=newIntent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 200);
Intentintent=newIntent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
Post a Comment for "Android Camera And Photo Picker Intent"