android 获取网络上服务器图片

本文详细讲解了在Android应用中如何从网络服务器获取并加载图片,涵盖了网络请求库的使用、图片缓存策略以及UI显示等方面的关键步骤。
摘要由CSDN通过智能技术生成
layout文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_showpage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.yuyu.graduationadmin.Page.store.showpage">
<ImageView
    android:id="@+id/Image_license"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

class

public class showpage extends AppCompatActivity {

    private String picture_String;
    private ImageView Image_license;
    private Bitmap img;

    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showpage);
        Image_license = (ImageView) findViewById(R.id.Image_license);
        Intent intent = new Intent();
        Bundle bundle = intent.getExtras();
        picture_String = bundle.getString("license");

        new DownloadImage().execute();
    }
    private Bitmap bitmap;

    class DownloadImage extends AsyncTask<String, Void, Object> {

    
        @Override
        protected Object doInBackground(String... string) {
            try {
                String PATH = "网络上照片的地址";
                URL url = new URL(picture_String);

                // 打开一个连接
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setConnectTimeout(5000);
                httpURLConnection.setRequestMethod("GET");

                if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    // 得到服务器返回过来的流对象
                    InputStream inputStream = httpURLConnection.getInputStream();
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    return bitmap;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Object object) {
            super.onPostExecute(object);

            if (object != null) {
                bitmap = (Bitmap) object;

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Image_license.setImageBitmap(bitmap);
                    }
                }, 2000);
            } else {
                //失败
                Toast.makeText(showpage.this, "下载失败", Toast.LENGTH_LONG).show();
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值