This is simple technique. You should use AlertDialog.Builder
for this purpose to display 3 options like:
1) Take Picture from camera
2) Choose from gallery
3) Cancel
and then just follow the steps below
See code below for help...
ImageView profile_img; //When you select image either from Gallery or take from Camera the code will set your selected image to this ImageView...
Context context;
final CharSequence[] items = { "Take Picture from camera", "Choose from gallery", "Cancel" };
private static final int REQUEST_CAMERA = 0;
private static final int SELECT_IMAGE = 1;
Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.your_xml_layout_name);
context = YourActivityClass.this;
profile_img = (ImageView) findViewById(R.id.profile_img);
profile_img.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
int id = v.getId();
switch (id)
{
case R.id.profile_img:
selectImage();
break;
}
}
private void selectImage()
{
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Choose Options...");
alert.setItems(items, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int position)
{
if(items[position].equals("Take Picture from camera"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
else if(items[position].equals("Choose from gallery"))
{
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SELECT_IMAGE);
}
else if(items[position].equals("Cancel"))
{
dialog.dismiss();
}
}
});
alert.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK)
{
if(requestCode == REQUEST_CAMERA)
{
Uri uri = (Uri) data.getData();
try
{
bmp = decodeUri(uri);
profile_img.setImageBitmap(bmp);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
else if(requestCode == SELECT_IMAGE)
{
Uri uri = data.getData();
try
{
bmp = decodeUri(uri);
profile_img.setImageBitmap(bmp);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
}
public String getPath(Uri uri)
{
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException
{
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(
getContentResolver().openInputStream(selectedImage), null, o);
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true)
{
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
{
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(
getContentResolver().openInputStream(selectedImage), null, o2);
}
Hope this will help you...Good Luck