Resize image generated by BitmapFactory.decodeByteArray ()

I create an audio player, I want to show the cover of the song for the player, it works with a small image, but if the mp3 file has a large image, it goes out of the layout. I am updating the image to 300x300 using the code below:

BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inDensity = 300; opt.inTargetDensity = 300; songCoverView.setImageBitmap(BitmapFactory.decodeByteArray(songCover, 0, songCover.length, opt)); 

But he still shows more and leaves the layout.

What is wrong with this code?

+4
source share
3 answers

try Bitmap.createScaledBitmap

bitmap = Bitmap.createScaledBitmap(songCover, 300, 300, true);

And you can keep the same aspect ratio for the old image ... I use the following logic:

  int width = songCover.getWidth(); int height = songCover.getHeight(); float scaleHeight = (float)height/(float)300; float scaleWidth = (float)width /(float)300; if (scaleWidth < scaleHeight) scale = scaleHeight; else scale = scaleWidth; bitmap = Bitmap.createScaledBitmap(songCover, (int)(width/scale), (int)(height/scale), true); 
+7
source

It turns out there is an error in Android: decodeByteArray somehow ignores some input parameters. A well-known workaround uses decodeStream with an input array enclosed in a ByteArrayInputStream as follows:

 BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inDensity = 300; opt.inTargetDensity = 300; songCoverView.setImageBitmap(BitmapFactory.decodeStream(new ByteArrayInputStream(songConver), null, opt)); 
+7
source

you can use the bitmap property

 Bitmap bitmap = Bitmap.createScaledBitmap(image, (int)x, (int)y, true); 
0
source

All Articles