Please Check this answer :
public class ImageUploading extends Activity
{
Uri imageUri = null;
ImageButton btnSubmit ;
public void onCreate(Bundle onsavedInstantState)
{
super.onCreate(onsavedInstantState);
setContentView(R.layout.edit_profile);
btnSubmit = (ImageButton) findViewById(R.id.btnSubmit);
btnSubmit.setClickable(true);
btnSubmit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
File imgFile = new File(Environment.getExternalStorageDirectory(),"my_photo.png");
imageUri = Uri.fromFile(imgFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent,0);
}
});
}
@Override
public void onActivityResult(int RequestCode, int ResultCode, Intent imageIntent)
{
super.onActivityResult(RequestCode, ResultCode, imageIntent);
try
{
if(RequestCode == 0)
{
if(ResultCode == Activity.RESULT_OK)
{
getContentResolver().notifyChange(imageUri, null);
ContentResolver objContentResolver = getContentResolver();
Bitmap imgBitmap = android.provider.MediaStore.Images.Media.getBitmap(objContentResolver, imageUri);
Drawable imgDrawable = new BitmapDrawable(imgBitmap);
btnSubmit.setBackgroundDrawable(imgDrawable);
}
}
}
catch(Exception e)
{}
}
}
It will capture image on button click, and set this image as background image for that button itself.