在android中显示服务器图像的最佳方式

在android中显示服务器图像的最佳方式,android,imageview,Android,Imageview,我正在尝试用android显示图像。需要从服务器获取映像。确保存储在服务器上的图像适合所有android设备屏幕的最佳方法是什么。在显示XML的图像中,我需要显示一个文本视图(在图像下方),以便对图像进行简要描述。是否必须创建具有特定高度和宽度的图像,或者是否有其他方法?您应该在服务器上存储足够大的图像 // Know the required width of the image URL url = new URL(remotePath); URLConnection urlConnectio

我正在尝试用android显示图像。需要从服务器获取映像。确保存储在服务器上的图像适合所有android设备屏幕的最佳方法是什么。在显示XML的图像中,我需要显示一个文本视图(在图像下方),以便对图像进行简要描述。是否必须创建具有特定高度和宽度的图像,或者是否有其他方法?

您应该在服务器上存储足够大的图像

// Know the required width of the image
URL url = new URL(remotePath);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
inputStream = (InputStream) urlConnection.getContent();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, options);
int height = options.outHeight;
int width = options.outWidth;
int sampleSize = requiredWidth / width; // Calculate how you want to sample the images so you can keep the memory small
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, options);
imageView.setImageBitmap(bitmap);

希望这有帮助。

如果您想在该屏幕上仅显示其下方的图像和描述,您可以将2个组件放置在方向垂直的
线性布局中,并使用
线性布局的
布局权重属性,例如,您可以执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scaleType="center" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

您可以手动从服务器下载映像,也可以使用库,如或