簡(jiǎn)單實(shí)現(xiàn)Android讀取網(wǎng)絡(luò)圖片到本地
今天在網(wǎng)上看到了一個(gè)關(guān)于讀取網(wǎng)絡(luò)文件的小視頻,覺得不錯(cuò),拿來與大家分享
思路
具體的思路比較的簡(jiǎn)單,但是思想非常的單純。那就是輸入一個(gè)網(wǎng)址,點(diǎn)擊按鈕,將從網(wǎng)絡(luò)上獲取的一張圖片顯示到一個(gè)ImageView控件上。
這樣看來,我們需要用到的核心就是網(wǎng)絡(luò)操作了。說白了,就是讀取網(wǎng)絡(luò)流文件了。
代碼展示
首先是主界面的布局文件
<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, "請(qǐng)輸入正確的網(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)絡(luò)以XML的方式獲得一張圖片,并顯示到一個(gè)ImageView上 * 按鈕事件可以直接不注冊(cè)onClickListener,而使用這個(gè)方法 * @param view */ public void getPicture(View view) { String website = mEt_url.getText().toString(); if (website == null || website.equals("")) { Toast.makeText(this, "請(qǐng)輸入正確的網(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; /** *圖片服務(wù)的業(yè)務(wù)類 */ 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)絡(luò)數(shù)據(jù)失敗".getBytes(); } }
package com.example.utils; import java.io.ByteArrayOutputStream; import java.io.InputStream; /** *專門用于將輸入流轉(zhuǎn)換成一個(gè)字節(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é)
這里面的代碼是非常的簡(jiǎn)單的,我這里貼出代碼的主要的目的是為了展示分層的思想,以及重構(gòu)的藝術(shù)。
在代碼中我們看到了,創(chuàng)建了專門的類來完成專門的工作。而且不同的層次的類,處理的業(yè)務(wù)也是不一樣的。這樣有助于我們以面向?qū)ο蟮姆绞骄幊?,帶來更加清晰的邏輯?br />
- Android 網(wǎng)絡(luò)圖片查看顯示的實(shí)現(xiàn)方法
- Android讀取本地或網(wǎng)絡(luò)圖片并轉(zhuǎn)換為Bitmap
- Android 異步獲取網(wǎng)絡(luò)圖片并處理導(dǎo)致內(nèi)存溢出問題解決方法
- Android顯示網(wǎng)絡(luò)圖片實(shí)例
- Android 下載網(wǎng)絡(luò)圖片并顯示到本地
- Android使用線程獲取網(wǎng)絡(luò)圖片的方法
- 在Android的應(yīng)用中實(shí)現(xiàn)網(wǎng)絡(luò)圖片異步加載的方法
- Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽功能
- Android 讀取sdcard上的圖片實(shí)例(必看)
- Android sdcard實(shí)現(xiàn)圖片存儲(chǔ) 、聯(lián)網(wǎng)下載
- Android開發(fā)實(shí)現(xiàn)加載網(wǎng)絡(luò)圖片并下載至本地SdCard的方法
相關(guān)文章
Android 通過Messager與Service實(shí)現(xiàn)進(jìn)程間雙向通信案例詳解
這篇文章主要介紹了Android 通過Messager與Service實(shí)現(xiàn)進(jìn)程間雙向通信案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09android自定義Camera實(shí)現(xiàn)錄像和拍照
這篇文章主要為大家詳細(xì)介紹了android自定義Camera實(shí)現(xiàn)錄像和拍照功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android編程之電池電量信息更新的方法(基于BatteryService實(shí)現(xiàn))
這篇文章主要介紹了Android編程之電池電量信息更新的方法,主要基于BatteryService實(shí)現(xiàn)該功能,以實(shí)例形式分析了Android獲取電池電量的具體步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-11-11Android開源組件SlidingMenu側(cè)滑菜單使用介紹
這篇文章主要介紹了Android開源組件SlidingMenu側(cè)滑菜單使用介紹,本文給出了SlidingMenu的項(xiàng)目地址、使用代碼、使用配置、常用的一些屬性設(shè)置中文注解等內(nèi)容,需要的朋友可以參考下2015-01-01Android使用OkHttp進(jìn)行重定向攔截處理的方法
這篇文章主要介紹了Android使用OkHttp進(jìn)行重定向攔截處理的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08Android觸摸事件和mousedown、mouseup、click事件之間的關(guān)系
今天小編就為大家分享一篇關(guān)于Android觸摸事件和mousedown、mouseup、click事件之間的關(guān)系,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01Android基礎(chǔ)知識(shí)之單點(diǎn)觸摸
這篇文章主要為大家詳細(xì)介紹了Android基礎(chǔ)知識(shí)之單點(diǎn)觸摸,很簡(jiǎn)單的操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06Android實(shí)現(xiàn)簡(jiǎn)單時(shí)鐘View的方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單的時(shí)鐘View,關(guān)鍵點(diǎn)在Canvas的平移與旋轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08基于flutter?sound插件實(shí)現(xiàn)錄音與播放功能
這篇文章主要介紹了基于flutter?sound插件實(shí)現(xiàn)錄音與播放功能,介紹了如何錄音,如何播放本地和遠(yuǎn)程音頻文件,以及如何實(shí)現(xiàn)動(dòng)畫,在錄制完音頻文件后如何上傳,這些都是我們平常使用這個(gè)功能會(huì)遇到的問題。在使用的過程中遇到的問題也有列出,需要的朋友可以參考下2022-05-05