unknown os platform
how to get bitmap image from ImageView in android programmatically | Whats-online.info
whats-online-info

how to get bitmap image from ImageView in android programmatically


how
2017/01/22 / Science and Tutorials

Android code: step by step how to create a bitmap image from ImageView programmatically. See an example.





android code: get bitmap image from ImageView programmatically


Hi, for this post, I would like to show how to get a bitmap image from ImageView in android programmatically.



We can simply define a bitmap as a representation of a bitmap image (java.awt.Image) and ImageView as a view you only embed within an XML layout to display an image (or any drawable) on the screen.

now lets code.

code to convert ImageView to Bitmap

Bitmap bm=((BitmapDrawable)v.getDrawable()).getBitmap();

First we need to add an ImageView to our XML and initialize it on our java class as shown. We will create a function to demonstrate the above.

Entire code


Our Java class (MainActivity.java)

package info.whats_online.converttobitmap; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.widget.ImageView; public class MainActivity extends Activity {     private ImageView imgView,bitmap;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         imgView=(ImageView) findViewById(R.id.imgView);         bitmap=(ImageView) findViewById(R.id.bitmap);         //set view to bitmap image         bitmap.setImageBitmap(convertImageViewToBitmap(imgView));     }     //function to convert imageView to Bitmap     private Bitmap convertImageViewToBitmap(ImageView v){         Bitmap bm=((BitmapDrawable)v.getDrawable()).getBitmap();         return bm;     } }

Our XML Layout(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     >     <ImageView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/imgView"         android:src="@drawable/call"/>     <ImageView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/bitmap"        /> </LinearLayout>


For our example, we have used a function that we only pass our ImageView to it and returns a bitmap image as shown above.

Thank you for your time, subscribe to our newsletter to recieve the latest updates direct to your mailbox
 












You may also like:


Leave a comment












Subscribe to get replies direct to your mailbox

Submit comment