dimanche 21 septembre 2014

Get ImageView bitmap


Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
Be carefull to check if your image.getDrawable() can actually be cast to BitmapDrawable (to avoid ClassCastExceptions). 
If, for instance, you use layers in your image then this snippet will be slightly different: Bitmap bitmap = ((BitmapDrawable)((LayerDrawable)image.getDrawable()).getDrawable(0)).getBitmap(‌​);
http://stackoverflow.com/questions/8306623/get-bitmap-attached-to-imageview

samedi 13 septembre 2014

Manage audio volume in my app

private AudioManager audio;
Inside onCreate:
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Override onKeyDown:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
        return true;
    default:
        return false;
    }
}