android圖片尺寸獲取
阿新 • • 發佈:2019-02-02
ps:在圖片展示中有時會遇到要獲取圖片寬高的情況!
1、本地圖片尺寸獲取
BitmapFactory.Options options=new BitmapFactory.Options();
/**獲取資原始檔中圖片尺寸*/
Bitmap bmp=BitmapFactory.decodeResource(getResources(),R.drawable.oldkids_banner_default,options);
/**獲取assets檔案中圖片尺寸**/
InputStream is= null;
try {
is = getAssets().open("icon_default.png");
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bmp2=BitmapFactory.decodeStream(is);
options.inJustDecodeBounds=true;
/**獲取記憶體卡中圖片尺寸**/
Bitmap bmp3=BitmapFactory.decodeFile("/storage/emulated/0/UCDownloads/pictures/pic_uc_1493011366435.jpg" ,options);
2、網路圖片尺寸獲取
這裡利用第三方圖片載入庫Glide 來獲取圖片的寬高。
Glide.with(context)
.load(url)
.asBitmap()
//設定獲取Bitmap
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
//通過拿到的bitmap獲取圖片寬高
int width=bitmap.getWidth();
int height=bitmap.getHeight();
/**操作寬高需要通過msg將值傳遞出去,不能直接在該方法中直接操作**/
}
});