HttpURLConnection获取server端图片显示并下载

MainActivity

public class MainActivity extends Activity {

    private static final String url = "http://127.0.0.1:65000/Tulips.jpg";

    Button download;
    ImageView show;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        show = findViewById(R.id.show);
        download = findViewById(R.id.download);

        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyAsyncTaskAs sync = new MyAsyncTaskAs(MainActivity.this,show);
                sync.execute(url);
            }
        });
    }
}

MyAsyncTask

class MyAsyncTask extends AsyncTask<String,Void,Bitmap> {

    private ImageView view;
    private MainActivity mainActivity;

    public MyAsyncTask(MainActivity mainActivity, ImageView view){
        this.view = view;
        this.mainActivity = mainActivity;
    }

    //onPreExecute用于异步处理前的操作
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    //在doInBackground方法中进行异步任务的处理.
    @Override
    protected Bitmap doInBackground(String... params) {
        Bitmap bitmap = null;
        try
        {
            // 定义一个URL对象
            URL url = new URL(params[0]);
            //打开url对应的网络资源
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            // 打开该URL对应的资源的输入流
            InputStream is = connection.getInputStream();
            // 从InputStream中解析出图片
            bitmap = BitmapFactory.decodeStream(is);
            //关闭输入流
            is.close();
            // 再次打开URL对应的资源的输入流
            connection = (HttpURLConnection)url.openConnection();
            is = connection.getInputStream();
            // 打开手机文件对应的输出流
            File file = new File("/mnt/sdcard/test.jpg");
            if(!file.exists()){
                file.createNewFile();
            }
            FileOutputStream os = new FileOutputStream(file);
            int hasRead = 0;
            byte[] buff = new byte[1024];
            // 将URL对应的资源下载到本地
            while((hasRead = is.read(buff)) > 0)
            {
                os.write(buff, 0 , hasRead);
            }
            is.close();
            os.close();
        }catch (MalformedURLException e) {
            Log.e("error","网络连接失败");
            e.printStackTrace();
        }catch (IOException e) {
            Log.e("error","图片下载失败");
            e.printStackTrace();
        }
        return bitmap;
    }

    //onPostExecute用于UI的更新.此方法的参数为doInBackground方法返回的值.
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        view.setImageBitmap(bitmap);
        Toast.makeText(mainActivity,"图片下载成功",Toast.LENGTH_SHORT).show();
    }
}

activity_main.xml

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/download"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="下载图片"/>

    <ImageView
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        />
    
</LinearLayout >

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值