Preserve Bitmap dimensions when saving to file from ImageView

128 Views Asked by At

I am trying to save the drawable inside of a ImageView to a file. I use Glide to load an image into the ImageView like this:

    Glide.with(context)
            //items[position] is a string that represents a url 
            .load(items[position])
            .listener(object : RequestListener<Drawable> {
                override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                    holder.progressBar.visibility = View.GONE
                    return false
                }

                override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                    holder.progressBar.visibility = View.GONE
                    return false
                }

            })
            .into(holder.imageView)

If the ImageView contains a GIF, I can save it without trouble to a file using the following method:

//In a typical use case, gifDrawable will equal holder.imageView.(gifDrawable.constantState.newDrawable().mutate()) as GifDrawable
private fun gifDrawableToFile(gifDrawable: GifDrawable, gifFile: File) {
    val byteBuffer = gifDrawable.buffer
    val output = FileOutputStream(gifFile)
    val bytes = ByteArray(byteBuffer.capacity())
    (byteBuffer.duplicate().clear() as ByteBuffer).get(bytes)
    output.write(bytes, 0, bytes.size)
    output.close()
}

However, if the ImageView contains a static image - then the dimensions are not preserved when I save it to a file using the following method:

//In a typical use case, bitmapDrawable will equal holder.imageView.drawable.constantState.newDrawable().mutate() as BitmapDrawable
bitmapDrawable.bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)

The above code fails to maintain the dimensions the static image had inside of the imageview.

For example - notice how the GIF maintains the dimensions that it had inside of the imageView:

Screenshot_Of_Gif

But notice here how the static image has drastically expanded in size:

Screenshot_Of_Static_Image

(In both these examples, I am sending a file to the Facebook Messenger app, which it uses to display an image inside of a chat).

Although my code samples are in Kotlin, answers in Java would be helpful too.

Update 1 - I printed the width of the ImageView and of the Bitmap with the following code and they are the same.

val bitmap = (holder.imageView.drawable as BitmapDrawable).bitmap
Log.d("APP","Bwidth: " + bitmap.width)
Log.d("APP","Bheight:" + bitmap.height)

Log.d("APP","Iwidth" + holder.imageView.width)
Log.d("PP","Iheight" + holder.imageView.height)

But the output of both heights/widths was 315, and 300.

Update 2 -

I printed the width of the bitmap after saving it to a file like this. The width and height were still 315 and 300.

val bmOptions = BitmapFactory.Options()
val bitmap = BitmapFactory.decodeFile(imageFile.absolutePath, bmOptions)
Log.d("APP","Bitmap from file width: " + bitmap.width)
Log.d("APP","Bitmap from file height: " + bitmap.height)
0

There are 0 best solutions below