代码之家  ›  专栏  ›  技术社区  ›  user8427383

从图像视图获取图像,并用编码位图将此图像发送到其他片段

  •  1
  • user8427383  · 技术社区  · 8 年前

    我要从服务器获取图像并放到这个图像视图我要从这个图像视图获取图像并用位图压缩并将这个图像发送到其他片段我不想为我的服务器添加2个请求

    我的代码是:

         Bitmap bmp = BitmapFactory.decodeResource(getResources(),"my problem");
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                mBundle.putByteArray("ResID", byteArray);
    

    我知道如果我放R.darwable.ic_午餐会有用的 但我想把这个图像从图像视图中提取出来放到“我的问题”中 你能帮我吗?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Sergey Emeliyanov    8 年前

    你可以用 mImageView.getDrawable() 得到 Drawable 对象来自 ImageView :

    Drawable drawable = mImageView.getDrawable();
    

    之后你可以获得 Bitmap 反对:

    BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
    Bitmap bitmap = bitmapDrawable.getBitmap();
    

    而且,如果你愿意,甚至 InputStream 字节数组发送它:

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    // Here, the CompressFormat and quality can be adjusted for output image size
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] imageBytes = stream.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
    

    因此, imageBytes 将包含图像的所有字节,您可以将其发送到其他位置(在您的场景中:发送到其他位置 Fragment )