这是您可以根据应用程序中的要求使用的方法。您还可以在 ImageView 上显示时重新调整图像大小。跳这会帮助你。
public Bitmap getBitmapFromAsset(String strName) {
AssetManager assetManager = getAssets();
InputStream istr = null;
try {
try {
istr = assetManager.open(strName);
} catch (FileNotFoundException e) {
istr = assetManager.open("noimage.png");
}
} catch (Exception e) {
// TODO: handle exception
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
int Height = bitmap.getHeight();
int Width = bitmap.getWidth();
float scale = getResources().getDisplayMetrics().density;
int dip = (int) (40 * scale + 0.5f);
int newHeight = width - dip;
int newWidth = width - dip;
float scaleWidth = ((float) newWidth) / Width;
float scaleHeight = ((float) newHeight) / Height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, Width, Height,
matrix, true);
return resizedBitmap;
}
调用方法如:
Imgview.setImageBitmap(getBitmapFromAsset("YourFoldername/"+ imgname + ".jpg"));