android 通过url下载图片,Android应用开发Android HttpURLConnection获取网络下载图片POST请求方式...

本文将带你了解Android应用开发Android HttpURLConnection获取网络下载图片POST请求方式,希望本文对大家学Android有所帮助。

"

Android HttpURLConnection获取网络下载图片POST请求方式

MainActivity.class

package cn.bgs.httpurlconnection;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

import android.os.Bundle;

import android.os.Environment;

import android.os.Handler;

import android.os.Message;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener

{

private Button mBtn;

private ImageView mImg;

private String

strPath=""https://p2.so.qhimg.com/sdr/720_1080_/t01a4d0d142596d4be7.jpg"";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}

private void initView() {

mBtn=(Button) findViewById(R.id.mBtn);

mImg=(ImageView) findViewById(R.id.mImg);

mBtn.setOnClickListener(this);

}

@Override

public void onClick(View v) {

new Thread(new Runnable() {

@Override

public void run() {

try {

URL url=new URL(strPath);

HttpURLConnection connection = (HttpURLConnection)

url.openConnection();

connection.setReadTimeout(50*1000);

connection.setConnectTimeout(50*1000);

connection.setDoInput(true);

connection.setDoOutput(true);

//设置请求方式

connection.setRequestMethod(""POST"");

InputStream is = connection.getInputStream();

String

path=Environment.getExternalStorageDirectory()+""/dameinv.jpg"";

File file=new File(path);

BufferedInputStream bis=new BufferedInputStream(is);

BufferedOutputStream bos=new BufferedOutputStream(new

FileOutputStream(file));

byte [] by=new byte[1024];

int len=0;

while((len=bis.read(by))!=-1){

bos.write(by, 0, len);

}

bos.flush();

bos.close();

bis.close();

is.close();

Bitmap bit=BitmapFactory.decodeFile(path);

Message msg=handler.obtainMessage();

msg.what=1;

msg.obj=bit;

handler.sendMessage(msg);

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}).start();

}

private Handler handler=new Handler(){

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

if (msg.what==1) {

Bitmap bitmap=(Bitmap) msg.obj;

mImg.setImageBitmap(bitmap);

}

}

};

}

XML

activity_main.XML

xmlns:tools=""https://schemas.android.com/tools""

android:layout_width=""match_parent""

android:layout_height=""match_parent""

tools:context="".MainActivity""

android:orientation=""vertical"">

android:id=""@+id/mBtn""

android:layout_width=""match_parent""

android:layout_height=""wrap_content""

android:text=""点击下载图片""

/>

android:id=""@+id/mImg""

android:layout_width=""match_parent""

android:layout_height=""match_parent""

/>

"

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值