A veces tenemos que mostrar la primera red para obtener la anchura de la imagen y la altura de los controles de imagen de hacer el proceso de carga, como por ejemplo para la visualización solamente una parte del cuadro demasiado tiempo, después de hacer clic para mostrar todo el mapa, a continuación, cómo obtener la imagen de la cara de red de la misma?
A modo usando HttpURLConnection + BitmapFactory.Options
OOM causados por el uso BitmapFactory.Options única manera de decodificar la frontera para evitar toda la imagen se cargará en los recursos de memoria y en consecuencia tomar demasiado ancho y la altura de la imagen
public static void getPicSize(String url, onPicListener listener) {
mPicFixThreadPool.execute(() -> {
HttpURLConnection connection;
try {
connection = (HttpURLConnection) new URL(url).openConnection();
InputStream inputStream = connection.getInputStream();
int[] imageSize = getImageSize(inputStream);
mMainHandler.post(() -> listener.onImageSize(imageSize[0], imageSize[1]));
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
});
}
private static int[] getImageSize(InputStream is) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
int[] size = new int[2];
size[0] = options.outWidth;
size[1] = options.outHeight;
LogUtil.i("--------------------width = " + size[0] + ",height = " + size[1]+"--------------------");
return size;
}
public interface onPicListener {
void onImageSize(int width, int height);
}
Segunda manera, el uso Glide
Glide.with(mContext).asBitmap().load(url).into(object : BitmapImageViewTarget(imageView) {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
super.onResourceReady(resource, transition)
val width = resource.width
val height = resource.height
Log.i("kkk", "width = $width,height = $height")
}
})
tres maneras
Para ser añadido ...