Captured Camera Image can't able to Crop - android

I am trying to get image from phone gallery or capturing image from camera..I have used 'me.villani.lorenzo.android:android-cropimage:1.1.+' for croping the image..It works well for getting image from phone gallery..While Capturing the image from camera,It captured the image but it cannot able to crop..It works fine on Choose from Image from gallery..Here i included my code,please have a look,
public class Details extends AppCompatActivity {
ImageView i1,i2;
Bitmap bitmapPic;
private static int REQUEST_PICTURE = 1;
private final static int REQUEST_PERMISSION_REQ_CODE = 34;
private static int REQUEST_CAMERA = 0, SELECT_FILE = 1, REQUEST_CROP_PICTURE = 2;
private static int CROP_IMAGE_SIZE = 200;
private static int CROP_IMAGE_HIGHLIGHT_COLOR = 0x6aa746F4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
android.support.v7.app.ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.hide();
}
i1 = (ImageView)findViewById(R.id.prof1);
i2 = (ImageView)findViewById(R.id.prof2);
i1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectImageOption();
}
});
}
private void selectImageOption() {
final CharSequence[] items = { "Capture Photo", "Choose from Gallery", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(Details.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Capture Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Gallery")) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("return-data", true);
startActivityForResult(intent, SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
File croppedImageFile = new File(getFilesDir(), "Pic.jpg");
Uri croppedImage = Uri.fromFile(croppedImageFile);
if (requestCode == REQUEST_CROP_PICTURE && resultCode == RESULT_OK) {
bitmapPic = BitmapFactory.decodeFile(croppedImageFile.getAbsolutePath());
if (bitmapPic != null) {
i1.setImageBitmap(bitmapPic);
} else {
Toast.makeText(Details.this, "Image Error while Cropping", Toast.LENGTH_LONG).show();
}
} else if (resultCode == RESULT_OK && (requestCode == REQUEST_CAMERA || requestCode == SELECT_FILE)) {
showImageCropView(data, croppedImage);
}
}
private void showImageCropView(Intent data, Uri croppedImage) {
CropImageIntentBuilder cropImage = new CropImageIntentBuilder(CROP_IMAGE_SIZE, CROP_IMAGE_SIZE, croppedImage);
cropImage.setOutlineColor(CROP_IMAGE_HIGHLIGHT_COLOR);
cropImage.setSourceImage(data.getData());
cropImage.setCircleCrop(true);
startActivityForResult(cropImage.getIntent(this), REQUEST_CROP_PICTURE);
}
}
Captured Image from Camera does not able to crop!Please give me better Solution for this.!Thanks in Advance

Bro, check this out. May solve your problem, you can crop the image from camera / gallery. link
set
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="#style/Base.Theme.AppCompat"/> <!-- optional (needed if default theme has no action bar) -->
to your manifest
call startCropImageActivity(null); in onclick method
this is the method :
private void startCropImageActivity(Uri imageUri) {
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.setMultiTouchEnabled(true)
.setRequestedSize(320, 320, CropImageView.RequestSizeOptions.RESIZE_INSIDE)
.setMinCropWindowSize(0,0)
.setAspectRatio(1,1)
.setCropShape(CropImageView.CropShape.OVAL)
.start(this);
}
and in onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
((CircleImageView) findViewById(R.id.profileImage)).setImageURI(result.getUri());
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Toast.makeText(this, "Cropping failed: " + result.getError(), Toast.LENGTH_LONG).show();
}
}
}
Here I'm using CircleImageView as circle image

Related

Selected from gallery or camera photos not displaying in the ImageView

Camera taken photo or selected photo is not displaying in ImageView. I'm using the following code:
private void attachFile() {
final CharSequence[] items = {"Camera", "Gallery", "Cancel"};
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(mContext);
builder.setTitle("Add Image");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if(items[i].equals("Camera")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[i].equals("Gallery")) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent,"Select Photo"), SELECT_FILE);
} else if (items[i].equals("Cancel")) {
dialogInterface.dismiss();
}
}
});
builder.show();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA && data != null) {
Log.d(TAG, "onActivityResult: done take photo");
Uri mImageCaptureUri = data.getData();
imageView.setImageURI(mImageCaptureUri);
/*Bundle bundle = data.getExtras();
final Bitmap bitmap = (Bitmap) bundle.get("data");
mOnphotoSelectedListener.getImageBitmap(bitmap);*/
} else if (requestCode == SELECT_FILE) {
Uri selectImageUri = data.getData();
mOnphotoSelectedListener.getImagePath(selectImageUri);
//imageView.setImageURI(selectImageUri);
//Picasso.with(mContext).load(selectImageUri).into(imageView);
//Picasso.with(this).load(selectImageUri).into(imageView);
}
}
}
i have this code i hope it will work for you its correct working
i set onclick for ImageProfile and i give to user picture + crop
i hope it will work for u
private void mImage_profileOnclick() {
int persmissonCheck = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA);
if (persmissonCheck == PackageManager.PERMISSION_DENIED) {
Req();
}else {
Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dialog_open_device);
Button pic, gal;
pic = (Button) dialog.findViewById(R.id.pick);
gal = (Button) dialog.findViewById(R.id.gallery);
pic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);
dialog.cancel();
dialog.dismiss();
}
});
gal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, 1);
dialog.cancel();
dialog.dismiss();
}
});
//dialog.create();
dialog.show();
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent
imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK) {
Bundle extras = imageReturnedIntent.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.cropeimage);
CropImageView cropImageView = (CropImageView) dialog.findViewById(R.id.cropImageView);
Button button = (Button) dialog.findViewById(R.id.bac);
cropImageView.setImageBitmap(imageBitmap);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mImage_profile.setImageBitmap(cropImageView.getCroppedImage());
dialog.cancel();
dialog.dismiss();
}
});
//dialog.create();
dialog.show();
}
break;
case 1:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
Dialog dialogs = new Dialog(getActivity());
dialogs.setContentView(R.layout.cropeimage);
CropImageView cropImageView = (CropImageView) dialogs.findViewById(R.id.cropImageView);
cropImageView.setImageUriAsync(selectedImage);
Button button = (Button) dialogs.findViewById(R.id.bac);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bitmap cropped = cropImageView.getCroppedImage();
mImage_profile.setImageBitmap(cropped);
dialogs.cancel();
dialogs.dismiss();
}
});
//dialogs.create();
dialogs.show();
}
break;
}
}
//#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void Req() {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CAMERA)) {
Toast.makeText(getActivity(), "شما مجوز استفاده از دوربین یا هر چیز دیگر را به برنامه نداده اید", Toast.LENGTH_SHORT).show();
}
else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 1);
mImage_profileOnclick();
} }
Used below code inside the onActivityResult
if (requestCode == Activity.RESULT_OK) {
File user_dp;
if (requestCode == REQUEST_CAMERA && data != null) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(), 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();
}
user_dp = destination;
//update_profilepic is ImageView reference
Picasso.with(getContext()).load(user_dp).into(update_profilepic);
} else if (requestCode == SELECT_FILE) {
Bitmap bm = null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
//ivImage is image reference
ivImage.setImageBitmap(bm);
}
}
}
Instead of resultCode I have requestCode on the first line inside the if statement.

Image not displaying on ImageView

Image is not displaying in the ImageView. I can select image or take a photo of a new image but the image will not show in ImageView.
public class RequestQuoteFragment extends Fragment {
ImageButton mPostImage;
ImageView imageView;
private EditText mDescription, mWidth, mLength, mHeight;
Integer REQUEST_CAMERA = 1, SELECT_FILE = 2;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_request_quotes, container, false);
imageView = view.findViewById(R.id.image_view);
mDescription = view.findViewById(R.id.item_description);
mWidth = view.findViewById(R.id.width);
mLength = view.findViewById(R.id.length);
mHeight = view.findViewById(R.id.height);
mPostImage = view.findViewById(R.id.attach_file);
mPostImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
attachFile();
}
});
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
return view;
}
//Attach Activity to fragments
private Context mContext;
#Override
public void onAttach(final Activity activity) {
super.onAttach(activity);
mContext = activity;
}
private void attachFile() {
final CharSequence[] items = {"Camera", "Gallery", "Cancel"};
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(mContext);
builder.setTitle("Add Image");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if(items[i].equals("Camera")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[i].equals("Gallery")) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent.createChooser(intent,"Select Photo"), SELECT_FILE);
} else if (items[i].equals("Cancel")) {
dialogInterface.dismiss();
}
}
});
builder.show();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
Bundle bundle = data.getExtras();
final Bitmap bitmap = (Bitmap) bundle.get("data");
imageView.setImageBitmap(bitmap);
} else if (requestCode == SELECT_FILE) {
Uri selectImageUri = data.getData();
imageView.setImageURI(selectImageUri);
}
}
}
//reset fields
private void resetFields() {
//UniversalImageLoader.setImage("", mPostImage);
mDescription.setText("");
mWidth.setText("");
mLength.setText("");
mHeight.setText("");
}
}
Loading image using Glide is as follows:
https://github.com/codepath/android_guides/wiki/Displaying-Images-with-the-Glide-Library
This one worked for me.
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT).setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(getIntent, "Select Profile Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});
startActivityForResult(chooserIntent, AppConstants.PICK_IMAGE);
And
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK && requestCode == AppConstants.PICK_IMAGE)
{
image.setImageURI(data.getData());
}
}
Edited
Otherwise
startActivityForResult(intent.createChooser(intent,"Select Photo"), SELECT_FILE);
Change above line to
startActivityForResult(Intent.createChooser(intent,"Select Photo"), SELECT_FILE);
There a small mistake on your code, that you have to check, resultCode == Activity.RESULT_OK, instead of requestCode == Activity.RESULT_OK
Hope it will help your issue. Thanks!
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
Bundle bundle = data.getExtras();
final Bitmap bitmap = (Bitmap) bundle.get("data");
imageView.setImageBitmap(bitmap);
} else if (requestCode == SELECT_FILE) {
Uri selectImageUri = data.getData();
imageView.setImageURI(selectImageUri);
}
}
}

Android won't allow me to get a photo from the gallery

Can anyone please explain to me why this code allows me to take a photo and show it in an image button but not when i try to get the picture from the gallery . It doesn't show any errors .
final CharSequence[] items = {"Take Photo", "Choose from Library", "Cancel"};
final AlertDialog.Builder constructor = new AlertDialog.Builder(this);
constructor.setTitle("Añadir Foto!");
constructor.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
PROFILE_PIC_COUNT = 1;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Library")) {
PROFILE_PIC_COUNT = 1;
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,SELECT_FILE);
} else if (items[item].equals("Cancel")) {
PROFILE_PIC_COUNT = 0;
dialog.dismiss();
}
}
});
img_header.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
constructor.show();
}
});
This is the onActivityResult method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent_imagen) {
super.onActivityResult(requestCode, resultCode, intent_imagen);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
setFoto(intent_imagen);
}
break;
case 1:
if(resultCode == RESULT_OK){
setFoto(intent_imagen);
}
break;
}
}
private void setFoto(Intent intent_imagen) {
Bitmap foto;
foto=getCroppedBitmap(Bitmap.createScaledBitmap((Bitmap)intent_imagen.getExtras().get("data"), 190, 165, true));
img_header.setForeground(null);
img_header.setImageBitmap(foto);
}

Two onActivityResults in one activity

Do you know how to handle two onActivityResult()s in one activity?
I need to use my camera and search for my photos in one activity.
public class MainActivity extends AppCompatActivity {
public static final int REQUEST_CAPTURE = 1;
Button button_Vyber_Fotku, button_Fotak;
ImageView imageView_VyberFotku, imageView_Fotak;
private static final int PICK_IMAGE = 100;
Uri imageUri_vybrana;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView_VyberFotku = (ImageView) findViewById(R.id.imageView_VyberFotku);
button_Vyber_Fotku = (Button) findViewById(R.id.button_Vyber_Fotku);
imageView_Fotak = (ImageView) findViewById(R.id.imageView_Fotak);
button_Fotak = (Button) findViewById(R.id.button_fotak);
if (!hasCamera())
{
button_Fotak.setEnabled(false);
}
}
public void Vyber_fotku_clicked(View v)
{
openGallery();
}
private void openGallery()
{
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
imageUri_vybrana = data.getData();
imageView_VyberFotku.setImageURI(imageUri_vybrana);
}
}
public boolean hasCamera()
{
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
public void PouzijFotakClicked(View v)
{
Intent vyfot = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(vyfot , REQUEST_CAPTURE);
}
#Override
protected void onActivityResult(int requestCode1, int resultCode1, Intent data1)
{ if (requestCode1 == REQUEST_CAPTURE && resultCode1 == RESULT_OK)
{
Bundle extras = data1.getExtras();
Bitmap photo = (Bitmap) extras.get("data1");
imageView_Fotak.setImageBitmap(photo);
}
}
}
Instead of two different method for onActivityResults use single method and distinguish them according to their request code.
#Override
protected void onActivityResult(int requestCode1, int resultCode1, Intent data1){
if (requestCode1 == REQUEST_CAPTURE && resultCode1 == RESULT_OK){
Bundle extras = data1.getExtras();
Bitmap photo = (Bitmap) extras.get("data1");
imageView_Fotak.setImageBitmap(photo);
}
else if (resultCode1 == RESULT_OK && requestCode1 == PICK_IMAGE){
imageUri_vybrana = data1.getData();
imageView_VyberFotku.setImageURI(imageUri_vybrana);
}
}
Note: You can't have two declaration for single override method.
// define two variable camera and pick_image of int type pass value of request code of desired out put in activity onResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
//first one to pick image
//do somthing
}else if(resultCode == RESULT_OK && requestCode == Camera){
//use to take image from camera response
}
}
Check this
It works for me
private static final int CAMERA_ = 999;
private static final int GALLERY_ = 888;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_) {
if (resultCode == RESULT_OK) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
UtilsClass.mBitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
imageView.setImageBitmap(UtilsClass.mBitmap);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}else if (requestCode == GALLERY_) {
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
UtilsClass.mBitmap = (BitmapFactory.decodeFile(picturePath));
imageView.setImageBitmap(UtilsClass.mBitmap);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}

image view:second image is replaced by the first image captured

hello i am struck in this coding first of all i am having two image view's when we click one of them the chooser dialog box will open which is having two option 1.take photo 2.select from gallery when i select from galley everything is working fine where as the problem is,
when we select take photo option and after we had captured the image when setting its preview for the first image its working fine where as the problem here is in the second image. when we select take photo option and after capturing the photo it is replacing the image of the first image with the second image.
This is Java Code:
public class PrescriptionUpload extends ActionBarActivity {
ImageView uploadimage1, uploadimage2;
Bitmap imagemap, imagemapsec;
int imageupload = 1;
int imagesecpload = 2;
int requestcamera = 0;
Button uploadpric;
StringBuilder sb;
String uploadimg2;
Toolbar include2;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prescriptionimages);
include2= (Toolbar) findViewById(R.id.include2);
setSupportActionBar(include2);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
uploadimage1 = (ImageView) findViewById(R.id.uploadimage1);
uploadimage1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectimage(imageupload);
}
});
uploadimage2 = (ImageView) findViewById(R.id.uploadimage2);
uploadimage2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectimage(imagesecpload);
}
});
}
private void selectimage(final int number) {
final CharSequence[] items = {"Take Photo", "Choose from Gallery"};
AlertDialog.Builder builder = new AlertDialog.Builder(PrescriptionUpload.this);
builder.setTitle("Add Prescription");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, requestcamera);
} else if (items[item].equals("Choose from Gallery")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
number);
}
}
});
builder.show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
if (requestCode == imageupload) {
try {
imagemap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
uploadimage1.setImageBitmap(imagemap);
uploadimage2.setVisibility(View.VISIBLE);
} catch (IOException e) {
e.printStackTrace();
}
} else if (requestCode == imagesecpload) {
try {
imagemapsec = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImageUri);
uploadimage2.setImageBitmap(imagemapsec);
} catch (IOException e) {
e.printStackTrace();
}
} else if (requestCode == requestcamera) {
if (uploadimage1.getId()==R.id.uploadimage1){
Bitmap testurl = (Bitmap) data.getExtras().get("data");
uploadimage1.setImageBitmap(testurl);
uploadimage2.setVisibility(View.VISIBLE);
}else if (uploadimage2.getId()==R.id.uploadimage2){
Bitmap testurltwo = (Bitmap) data.getExtras().get("data");
uploadimage2.setImageBitmap(testurltwo);
}
}
}
}
i had solved my problem using onActivityResult and with the increment of the variable

Categories

Resources