HttpURLConnection下載圖片的兩種方式

來源:互聯網
上載者:User

標籤:stack   yum   url   row   exce   tail   ring   click   lis   

public class MainActivity extends AppCompatActivity {

private ImageView iv;

private String imageurl = "http://img06.tooopen.com/images/20161106/tooopen_sl_185050524199.jpg";
private Bitmap bitmap;

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

iv = (ImageView) findViewById(R.id.iv_show);

findViewById(R.id.load).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(loadrunable).start();
}
});
}


private Runnable loadrunable = new Runnable() {

private InputStream is;

@Override
public void run() {

try {
URL imgUrl = new URL(imageurl);
// 使用HttpURLConnection開啟串連
HttpURLConnection urlConn = (HttpURLConnection) imgUrl
.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(false);
urlConn.setRequestMethod("GET");
urlConn.setConnectTimeout(3000);
urlConn.setUseCaches(true);
urlConn.connect();
int code = urlConn.getResponseCode();
Log.e("tag", "run: "+code );
// 將得到的資料轉化成InputStream
InputStream is = urlConn.getInputStream();
// 將InputStream轉換成Bitmap
// bitmap = getBitmapInputStream(is);

byte[] bytesInputStream = getBytesInputStream(is);
bitmap = BitmapFactory.decodeByteArray(bytesInputStream,0,bytesInputStream.length);

Message msgone = new Message();
msgone.what = 1;
handler.sendMessage(msgone);


} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {

if (null != is){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
};

private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// super.handleMessage(msg);
Log.e("tag", "handleMessage: "+msg.what );

if (null != bitmap && null != iv){
iv.setImageBitmap(bitmap);
}
}
};

public Bitmap getBitmapInputStream(InputStream is){
Bitmap bp;
bp = BitmapFactory.decodeStream(is);

return bp;
}

public byte[] getBytesInputStream( InputStream is) throws IOException {

ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
byte[] buff = new byte[512];
int len;
while ((len = is.read(buff))!= -1){

arrayOutputStream.write(buff,0,len);
}

is.close();
arrayOutputStream.close();

return arrayOutputStream.toByteArray();

}
}

重點:不要設定setDoOutput(true),post請求上傳參數得設定為true;
它預設為false: urlConn.setDoOutput(false);

參考部落格: http://blog.csdn.net/ameyume/article/details/6528205

HttpURLConnection下載圖片的兩種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.