1

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)
Foobar
  • 7,458
  • 16
  • 81
  • 161
  • Download the image to a file (e.g., using OkHttp), then worry about loading the file into the `ImageView`. – CommonsWare Jul 10 '18 at 23:16
  • I prefer to use glide - it handles stuff like putting an image into a `ImageView` and caching. Regardless of OkHttp vs Glide, it should be possible to convert a `Bitmap` to a file without losing its dimensions. – Foobar Jul 11 '18 at 00:12
  • If you look at the `Bitmap` object, I suspect that you will find that it has the dimensions that are shown in the file. You appear to have assumed that the `Bitmap` will have the dimensions of the `ImageView`. – CommonsWare Jul 11 '18 at 00:16
  • That's what I thought too, but the dimensions of the `Bitmap` and the dimensions of the `ImageView` are the same. I've updated my post with the code that outputs the dimensions of `ImageView` and `Bitmap` – Foobar Jul 11 '18 at 00:21
  • What are the dimensions of the image in the file itself? And, if you manually download the image from the URL, what are the dimensions of the original image? I have never attempted to work with a `Bitmap` managed by a `BitmapDrawable`, so it is possible that "the rules of the game" are different for it somehow, though I'm not quite certain how. – CommonsWare Jul 11 '18 at 00:22
  • Glide downloads the image from the following url: https://github.com/vedantroy/Image-Database/raw/master/Curated/Hype/akyuusqueel.jpg The dimensions of this image are 129 x 123 (pixels). With a horizontal and vertical resolution of 72 dpi and resolution unit of 2, whatever that means. – Foobar Jul 11 '18 at 00:26
  • OK. If the file that you are saving via `outputStream` has a 315x300 image, then the problem lies somewhere in Facebook Messenger. If the file that you are saving has larger dimensions, then poke at some of the more obscure properties of the bitmap and see if anything appears to be unusual (e.g., `isPremultiplied()`). – CommonsWare Jul 11 '18 at 00:30
  • I checked the dimensions of the file saved via `outputStream`. It has the correct dimensions (315 x 300). So, the issue might be with Fb messenger and Telegram.That being said there is another image that does not exhibit this problem behavior: https://github.com/vedantroy/Image-Database/raw/master/Curated/Lewd/yandereyuno.jpg so I'm going to see if there are any differences in the bitmaps of those two files. Are there any other obscure bitmap properties besides `isPremultiplied()` that I should look into? – Foobar Jul 11 '18 at 00:43
  • When I scanned through the `Bitmap` docs before posting that comment, the only one that leaped out at me that might be related to size was `isPremultiplied()`. AFAIK, that is an Android thing, not a PNG thing, and so I would not expect it to have an impact on how the PNG is encoded. You might also consider experimenting with writing out JPEG files and see if that changes anything. JPEG might be a better format anyway, if you are not completely in control over the images. – CommonsWare Jul 11 '18 at 10:58

0 Answers0