簡單實現(xiàn)Android讀取網(wǎng)絡圖片到本地
今天在網(wǎng)上看到了一個關(guān)于讀取網(wǎng)絡文件的小視頻,覺得不錯,拿來與大家分享
思路
具體的思路比較的簡單,但是思想非常的單純。那就是輸入一個網(wǎng)址,點擊按鈕,將從網(wǎng)絡上獲取的一張圖片顯示到一個ImageView控件上。
這樣看來,我們需要用到的核心就是網(wǎng)絡操作了。說白了,就是讀取網(wǎng)絡流文件了。
代碼展示
首先是主界面的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/et_website"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="please type the url "
/>
<Button
android:id="@+id/btn_get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check"
/>
<ImageView
android:id="@+id/iv_picture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
</LinearLayout>
然后是主界面的邏輯代碼
package com.example.getphotobyxml;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.service.ImageService;
public class MainActivity extends Activity {
private EditText mEt_url;
private ImageView mIv_picture;
private Button mBtn_get;
/**
* 初始化相關(guān)的需要使用到的ID
*/
public void init() {
mEt_url = (EditText) findViewById(R.id.et_website);
mIv_picture = (ImageView) findViewById(R.id.iv_picture);
mBtn_get = (Button) findViewById(R.id.btn_get);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//記得要調(diào)用哦
init();
mBtn_get.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String website = mEt_url.getText().toString();
if (website == null || website.equals("")) {
Toast.makeText(MainActivity.this, "請輸入正確的網(wǎng)址哦!",
Toast.LENGTH_LONG).show();
return;
}
byte[] bytes;
try {
bytes = ImageService.getImage(website);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
bytes.length);
mIv_picture.setImageBitmap(bitmap);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
/**
* 從網(wǎng)絡以XML的方式獲得一張圖片,并顯示到一個ImageView上
* 按鈕事件可以直接不注冊onClickListener,而使用這個方法
* @param view
*/
public void getPicture(View view) {
String website = mEt_url.getText().toString();
if (website == null || website.equals("")) {
Toast.makeText(this, "請輸入正確的網(wǎng)址哦!", Toast.LENGTH_LONG).show();
return;
}
byte[] bytes;
try {
bytes = ImageService.getImage(website);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
bytes.length);
mIv_picture.setImageBitmap(bitmap);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
service 以及 tools助手
package com.example.service;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.example.utils.StreamTool;
/**
*圖片服務的業(yè)務類
*/
public class ImageService {
public static byte[] getImage(String website) throws Exception {
URL url = new URL(website);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200){
InputStream inputStream = conn.getInputStream();
byte[] bytes = StreamTool.read(inputStream);
return bytes;
}
return "讀取網(wǎng)絡數(shù)據(jù)失敗".getBytes();
}
}
package com.example.utils;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
/**
*專門用于將輸入流轉(zhuǎn)換成一個字節(jié)數(shù)組的utils類
*/
public class StreamTool {
public static byte[] read(InputStream inputStream) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len = inputStream.read(buf))!=-1){
baos.write(buf, 0 ,len);
}
baos.close();
return buf;
}
}
總結(jié)
這里面的代碼是非常的簡單的,我這里貼出代碼的主要的目的是為了展示分層的思想,以及重構(gòu)的藝術(shù)。
在代碼中我們看到了,創(chuàng)建了專門的類來完成專門的工作。而且不同的層次的類,處理的業(yè)務也是不一樣的。這樣有助于我們以面向?qū)ο蟮姆绞骄幊?,帶來更加清晰的邏輯?br />
- Android 網(wǎng)絡圖片查看顯示的實現(xiàn)方法
- Android讀取本地或網(wǎng)絡圖片并轉(zhuǎn)換為Bitmap
- Android 異步獲取網(wǎng)絡圖片并處理導致內(nèi)存溢出問題解決方法
- Android顯示網(wǎng)絡圖片實例
- Android 下載網(wǎng)絡圖片并顯示到本地
- Android使用線程獲取網(wǎng)絡圖片的方法
- 在Android的應用中實現(xiàn)網(wǎng)絡圖片異步加載的方法
- Android實現(xiàn)網(wǎng)絡圖片瀏覽功能
- Android 讀取sdcard上的圖片實例(必看)
- Android sdcard實現(xiàn)圖片存儲 、聯(lián)網(wǎng)下載
- Android開發(fā)實現(xiàn)加載網(wǎng)絡圖片并下載至本地SdCard的方法
相關(guān)文章
Android PopupWindow實現(xiàn)左側(cè)彈窗效果
這篇文章主要為大家詳細介紹了Android PopupWindow實現(xiàn)左側(cè)彈窗效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android中Rxjava實現(xiàn)三級緩存的兩種方式
這篇文章主要介紹了Android中Rxjava實現(xiàn)三級緩存的兩種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04
Android Camera2采集攝像頭原始數(shù)據(jù)
這篇文章主要介紹了Android Camera2采集攝像頭原始數(shù)據(jù)并進行手工預覽的功能實現(xiàn)原理以及代碼分析,需要的朋友學習下吧。2018-02-02
Android開發(fā)實現(xiàn)NFC刷卡讀取的兩種方式
這篇文章主要為大家詳細介紹了Android開發(fā)中實現(xiàn)NFC刷卡讀取的兩種方式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
android Matrix實現(xiàn)圖片隨意放大縮小或拖動
這篇文章主要為大家詳細介紹了android Matrix實現(xiàn)圖片隨意放大縮小或拖動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
Android使用Javamail發(fā)送Email群發(fā)加附件
這篇文章主要為大家詳細介紹了Android使用Javamail發(fā)送Email群發(fā)加附件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
Android開發(fā)筆記之:返回鍵的復寫onBackPressed()介紹
本篇文章是對Android中返回鍵的復寫onBackPressed()進行了詳細的分析介紹,需要的朋友參考下2013-05-05

