Reputation: 357
i managed to read album artwork from my mp3 file(on sdcard) but the picture is biger than activity. How can i downsize(compress) picture to 150x150 pixels?
Upvotes: 0
Views: 384
Reputation: 6159
The following piece of code will do the trick ;)
public static Bitmap decodeFile(File f, boolean goodQuality){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//The new size we want to scale to
final int REQUIRED_SIZE=100;
//Find the correct scale value. It should be the power of 2.
int scale=1;
if(!goodQuality){
while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
Upvotes: 0
Reputation: 104198
You can use "fitXY" to scale an ImageView:
<ImageView android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="fitXY" />
You can set the source of the ImageView programmatically.
Upvotes: 2