Given
ImageView image = R.findViewById(R.id.imageView); image.setImageBitmap(someBitmap); Is it possible to retrieve the bitmap?
Given
ImageView image = R.findViewById(R.id.imageView); image.setImageBitmap(someBitmap); Is it possible to retrieve the bitmap?
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); image.getDrawable() can actually be cast to BitmapDrawable (to avoid IllegalCastExceptions). 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();ImageView is set from URI? imageView.setImageUri()This will get you a Bitmap from the ImageView. Though, it is not the same bitmap object that you've set. It is a new one.
imageView.buildDrawingCache(); Bitmap bitmap = imageView.getDrawingCache(); === EDIT ===
imageView.setDrawingCacheEnabled(true); imageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); imageView.layout(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight()); imageView.buildDrawingCache(true); Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache()); imageView.setDrawingCacheEnabled(false); Bitmap bmap = Bitmap.createBitmap(mImageView.getDrawingCache());Write below code
ImageView yourImageView = (ImageView) findViewById(R.id.yourImageView); Bitmap bitmap = ((BitmapDrawable)yourImageView.getDrawable()).getBitmap(); For those who are looking for Kotlin solution to get Bitmap from ImageView.
var bitmap = (image.drawable as BitmapDrawable).bitmap This code is better.
public static byte[] getByteArrayFromImageView(ImageView imageView) { BitmapDrawable bitmapDrawable = ((BitmapDrawable) imageView.getDrawable()); Bitmap bitmap; if(bitmapDrawable==null){ imageView.buildDrawingCache(); bitmap = imageView.getDrawingCache(); imageView.buildDrawingCache(false); }else { bitmap = bitmapDrawable .getBitmap(); } ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); return stream.toByteArray(); } Bitmap imagenAndroid = BitmapFactory.decodeResource(getResources(),R.drawable.jellybean_statue);try this code:
Bitmap bitmap; bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();