如何从imageview获取图像以通过位图发布

时间:2013-12-18 13:15:30

标签: android bitmap imageview

图片视图所在的XML

<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>

在oncreate中我有

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

我的问题 ::

然后是Bitmap,用于发布如何从图像视图中获取图像

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

4 个答案:

答案 0 :(得分:2)

您可以这样使用:

Bitmap bm;

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

从imageView使用获取Drawable:

ImageVIew.getDrawable()

如果您想从以下的drawable使用输入流:

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);

答案 3 :(得分:1)

ImageView中首先将选项设置为true,然后将图像转换为位图后将其保存到SD卡中,然后再共享该图像。

尝试如下:

          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) {

        }