从图像视图获取图像并将此图像发送到其他带有编码位图的片段

Get Image From Image view and send This image to other fragment with encode bitmap

我想从服务器获取图像并放入此图像视图我想从此图像视图获取图像并使用位图压缩并将此图片发送到其他片段我不想为我的服务器添加 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_luncher 它会起作用 但我想从图像视图中获取此图像并将其放入“我的探测器” 你能帮帮我吗?

您可以使用 mImageView.getDrawable()ImageView 获取 Drawable 对象:

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