Ramakrishna
Ramakrishna

Reputation: 4086

How to convert a imageview to bitmap image in Android

I have an imageview.

  LoaderImageView image=new LoaderImageView(context,path1);

in above statement it returns a imageview. So I want to convert it into a bitmap. How can I convert imageview into bitmap image.

   ImageView i=new ImageView(context);
   i.setImageBitmap(convertimagetobitmap);

In above statement I have set a bitmap image (converted image) to another imageview.

Upvotes: 3

Views: 25154

Answers (4)

chikka.anddev
chikka.anddev

Reputation: 9629

Take a look this code:

// i is an imageview which you want to convert in bitmap
Bitmap viewBitmap = Bitmap.createBitmap(i.getWidth(),i.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(viewBitmap);

i.draw(canvas);

That's it, your imageview is stored in bitmap viewBitMap.

Upvotes: 8

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

This will work :

ImageView img;
img.setDrawingCacheEnabled(true);
Bitmap scaledBitmap = img.getDrawingCache();

//Store to tmp file

        String extr = Environment.getExternalStorageDirectory().toString();
        File mFolder = new File(extr + "/tmpfolder");
        if (!mFolder.exists()) {
            mFolder.mkdir();
        }

        String s = "tmp.png";

        File f = new File(mFolder.getAbsolutePath(), s);

        strMyImagePath = f.getAbsolutePath();
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(f);
            scaledBitmap.compress(Bitmap.CompressFormat.PNG, 70, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (Exception e) {

            e.printStackTrace();
        }

        scaledBitmap.recycle();
    } catch (Throwable e) {
    }

Upvotes: 0

namar0x0309
namar0x0309

Reputation: 2349

Check this BitmapFactory
This gets you access to bitmap where view is stored DrawingCache (cash has to be turned on)

Hope this helps!

Upvotes: 0

Barmaley
Barmaley

Reputation: 16363

Look at:

ImageView.getDrawingCache();

But you have to keep in mind that returned Bitmap won't be the same as you get from resource or file, since Bitmap will depend on display physical properties (resolution, scaling, color deepness and so on).

Upvotes: 0

Related Questions