我设法从我的mp3文件(在SD卡上)读取专辑封面,但照片是 比活动更多。如何将图片缩小(压缩)到150x150像素?
答案 0 :(得分:2)
您可以使用“fitXY”来缩放ImageView:
<ImageView android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="fitXY" />
您可以通过编程方式设置ImageView的来源。
答案 1 :(得分:0)
下面这段代码可以解决问题;)
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;
}