[英]How to get image width and height from a network url
我正在開發一個 android 應用程序並在其中使用 REST API。 我正在從服務器接收圖像 url,我想根據圖像的原始高度使用圖像 url 顯示這些圖像。
如何使用網絡圖像 url 獲取圖像的寬度和高度。 我已經嘗試從字符串 url 獲取位圖,但為此我想在后台線程中執行此操作,這會導致圖像加載速度非常慢。
有什么辦法可以從圖像url中獲取圖像的寬度和高度,並可以根據寬度和高度在imageview中顯示圖像。
如果有人知道這件事,請幫助我。
非常感謝先進。
URL url = new URL("url/image.jpg");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
對於寬度:
bmp.getWidth();
對於高度:
bmp.getHeight();
用於在 imageview 中設置:
ImageView.setImageBitmap(bmp);
進一步參考:
https://developer.android.com/reference/android/graphics/Bitmap.html#getHeight%28%29
/* your library */
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
/* your code */
Glide.with(this)
.asBitmap() //get hieght and width
.load("link image")
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable
Transition<? super Bitmap> transition) {
Log.d("TAG", "onResourceReady: "+resource.getWidth()+"
"+resource.getHeight());
}
});
//////////////////////////////////////
Glide.with(this)
.asFile() // get size Image
.load("link image")
.into(new SimpleTarget<File>() {
@Override
public void onResourceReady(@NonNull File resource, @Nullable
Transition<? super File> transition) {
fab_full.setLabelText(""+resource.length());
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.