Skip to content

Instantly share code, notes, and snippets.

@nitink133
Created June 26, 2018 16:24
Show Gist options
  • Save nitink133/eea6a5825a260623ee7dc69dceb40dd7 to your computer and use it in GitHub Desktop.
Save nitink133/eea6a5825a260623ee7dc69dceb40dd7 to your computer and use it in GitHub Desktop.
Android camera and gallery permissions
private void selectImage() {
try {
PackageManager pm = getPackageManager();
int hasPerm = pm.checkPermission(Manifest.permission.CAMERA, getPackageName());
if (hasPerm == PackageManager.PERMISSION_GRANTED) {
final CharSequence[] options = {"Take Photo", "Choose From Gallery", "Cancel"};
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.chooseimage_alertdialog_l, null);
builder.setView(dialogView);
TextView title = (TextView) dialogView.findViewById(R.id.alert_title);
TextView takephoto = (TextView) dialogView.findViewById(R.id.alert_takephoto);
TextView choosefromgallery = (TextView) dialogView.findViewById(R.id.alert_gallery);
TextView cancel = (TextView) dialogView.findViewById(R.id.alert_cancel);
Typeface typeface = Typeface.createFromAsset(
getAssets(),
"fonts/arean.ttf"
);
title.setTypeface(typeface);
takephoto.setTypeface(typeface);
choosefromgallery.setTypeface(typeface);
cancel.setTypeface(typeface);
android.support.v7.app.AlertDialog dialog = builder.create();
takephoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
choosefromgallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Intent pickPhoto = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, 1);
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
// builder.setItems(options, new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int item) {
// if (options[item].equals("Take Photo")) {
// } else if (options[item].equals("Choose From Gallery")) {
// dialog.dismiss();
// Intent pickPhoto = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// startActivityForResult(pickPhoto, 1);
// } else if (options[item].equals("Cancel")) {
// dialog.dismiss();
// }
// }
// });
// builder.show();
} else
Toast.makeText(this, "Camera Permission error", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Camera Permission error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/* Bitmap bitmap = ImagePickerUtils.getImageFromResult(LoanRequestActivity.this, resultCode, data);
if (bitmap != null) {
binding.loanImage.setBitmap(bitmap);
mPresenter.uploadUserDoc(bitmap);
}*/
if (requestCode == 0) {
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
// boolean isCamera = data.getExtras().getBoolean("isCamera");
Bitmap bitmap = null;
// if (isCamera)
bitmap = (Bitmap) data.getExtras().get("data");
// else
// bitmap = ImagePickerUtils.getImageFromResult(LoanRequestActivity.this, resultCode, data);
imageModel = new ImageUploadWrapper();
imageModel.setBitmap((bitmap));
imageModels.add(imageModel);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(context.getFilesDir(), "IMG_" + System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), destination);
MultipartBody.Part multipart = MultipartBody.Part.createFormData("image", destination.getName(),
requestFile);
mPresenter.uploadUserDoc(multipart);
if (imageModels != null) {
CustomImageLoader customImageLoader = new CustomImageLoader(LoanRequestActivity.this, imageModels);
customImageLoader.updateAdpter();
if (binding.loanImage.getParent() != null) {
binding.loanImage.removeAllViews();
binding.loanImage.addView(customImageLoader);
binding.loanImage.setVisibility(View.VISIBLE);
binding.loanDocument.setEditTextHint("Add More Documents if you have...");
binding.loanDocument.makeEditTextUnEditable();
}
}
}
}
} else if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
Uri selectedImage = data.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
} catch (IOException e) {
e.printStackTrace();
}
// if (isCamera)
// bitmap = (Bitmap) data.getExtras().get("data");
// else
// bitmap = ImagePickerUtils.getImageFromResult(LoanRequestActivity.this, resultCode, data);
imageModel = new ImageUploadWrapper();
imageModel.setBitmap((bitmap));
imageModels.add(imageModel);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(context.getFilesDir(), "IMG_" + System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), destination);
MultipartBody.Part multipart = MultipartBody.Part.createFormData("image", destination.getName(),
requestFile);
mPresenter.uploadUserDoc(multipart);
if (imageModels != null) {
CustomImageLoader customImageLoader = new CustomImageLoader(LoanRequestActivity.this, imageModels);
customImageLoader.updateAdpter();
if (binding.loanImage.getParent() != null) {
binding.loanImage.removeAllViews();
binding.loanImage.addView(customImageLoader);
binding.loanImage.setVisibility(View.VISIBLE);
binding.loanDocument.setEditTextHint("Add More Documents if you have...");
binding.loanDocument.makeEditTextUnEditable();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment