简体   繁体   English

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

[英]how to get the image from imageview for posting through bitmap

XML where image view is there 存在图像视图的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>

in oncreate i have 在oncreate我有

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

You can use this way: 您可以使用这种方式:

Bitmap bm;

BitmapDrawable drawable = (BitmapDrawable) yourimageview.getDrawable();
bm = drawable.getBitmap();
Bitmap bitmapOrg = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

for getting Drawable from imageView use: 用于从imageView获取Drawable使用:

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

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. 在您的ImageView ,首先将选项设置为true ,然后将该图像转换为位图后将其保存到sdcard中,然后共享该图像。

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

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM