smriti3
smriti3

Reputation: 871

how to get the image from imageview for posting through bitmap

XML where image view is there

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="101dp"
            android:layout_marginTop="32dp"
            android:clickable="false"
            android:src="@drawable/image" />


</LinearLayout>

in oncreate i have

imageView = (ImageView) findViewById(R.id.imageView1);

My Question::

then for Bitmap for posting how to get the image from image view

Bitmap bitmapOrg = BitmapFactory.decodeResource(------?----------);

Upvotes: 0

Views: 149

Answers (4)

Piyush
Piyush

Reputation: 18923

You can use this way:

Bitmap bm;

BitmapDrawable drawable = (BitmapDrawable) yourimageview.getDrawable();
bm = drawable.getBitmap();

Upvotes: 2

GrIsHu
GrIsHu

Reputation: 23638

In your ImageView set the option to true first and the save that image into sdcard after converting it to bitmap and then share that image.

Try as below:

          imageView = (ImageView) findViewById(R.id.imageView1);
          m_ivproimg.setDrawingCacheEnabled(true);
        Bitmap bitmap = m_ivproimg.getDrawingCache();

         //Save image into sdcard and given name randomly.   
        String root = Environment.getExternalStorageDirectory().toString();
        File newDir = new File(root + "/saved_images");
        newDir.mkdirs();
        Random gen = new Random();
        int n = 10000;
        n = gen.nextInt(n);
        String fotoname = "photo-" + n + ".jpg";
        File file = new File(newDir, fotoname);
        String s = file.getAbsolutePath();

        if (file.exists())
            file.delete();
        try {
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
        } catch (Exception e) {

        }

Upvotes: 1

The Ray of Hope
The Ray of Hope

Reputation: 738

for getting Drawable from imageView use:

ImageVIew.getDrawable()

If you want to get inputstream from the drawable use following:

BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
Bitmap bitmap = bitmapDrawable .getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);

Upvotes: 1

Damien R.
Damien R.

Reputation: 3373

Bitmap bitmapOrg = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

Upvotes: 1

Related Questions