1. 程式人生 > >通過URLHttpConnection方式來取得圖片,並且顯示在ImageView上

通過URLHttpConnection方式來取得圖片,並且顯示在ImageView上

<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"
    tools:context="com.zzw.watctImage.MainActivity" >

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:src="@drawable/t01a7b264978bb316f1"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/url_edit"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="http://p1.so.qhimg.com/t017cf2863df563308b.jpg" />

        <Button
            android:id="@+id/get"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="獲得圖片"
            android:textColor="@android:color/holo_blue_light" />
    </LinearLayout>

</LinearLayout>

XML

package com.zzw.watctImage;

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.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {
    ImageView image_view = null;
    EditText image_uri = null;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            if (msg.what == 0) {
                Bitmap bitmap = (Bitmap) msg.obj;
                image_view.setImageBitmap(bitmap);
            }

        }

    };
    /**
     * 1.訪問網路不能直接放在主方法裡面(android.os.NetworkOnMainThreadException), 應該放在一個執行緒裡面
     * 2.android.view.ViewRootImpl$CalledFromWrongThreadException:
     * 只能在主執行緒或者UI執行緒裡面修改檢視,應該用通訊來解決
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    // 初始化
    public void init() {
        image_view = (ImageView) findViewById(R.id.image_view);
        image_uri = (EditText) findViewById(R.id.url_edit);
        findViewById(R.id.get).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        final String uri = image_uri.getText().toString();
        new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                Bitmap bitmap = getBitmap(uri);
                if (bitmap != null) {
                    Message msg = new Message();
                    msg.what = 0;// 說明你是誰
                    msg.obj = bitmap;
                    handler.sendMessage(msg);
                }

            }
        }).start();
        ;
    }

    // bitmap------>點陣圖
    public Bitmap getBitmap(String uri) {
        HttpURLConnection conn = null;
        try {
            // 1、獲得圖片的url
            URL url = new URL(uri);

            // 2、獲得網路連線
            conn = (HttpURLConnection) url.openConnection();

            // 3、設定請求的一些常用引數
            conn.setReadTimeout(3000);// 設定連線去讀取資料的最長時間
            conn.setConnectTimeout(3000);// 設定超時
            conn.setDoInput(true);// 設定請求可以讓伺服器寫入資料

            // 4、真正的請求圖片,然後把從伺服器上請求的二進位制流儲存到inputStream裡面
            conn.connect();
            InputStream in = conn.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(in);
            return bitmap;
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 5、關閉網路連線
        finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
        return null;
    }
}

JAVA


相關推薦

通過URLHttpConnection方式取得圖片並且顯示ImageView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" andro

java傳多張圖片並且可以刪除傳的圖片

如果你想只上傳一張圖片:http://blog.csdn.net/xuanzhangran/article/details/54928997 上傳多張: 可以刪除從庫裡查詢出來的圖片,也可以刪除剛上傳到圖片,其實不多,為了任務請耐心看完: 效果如下: 1

JAVA中建立HTTP通訊從伺服器獲取HTML程式碼通過HTTP請求下載圖片或其他二進位制檔案的程式下載結果要按下載到的檔案型別進行存檔中。

通過HTTP請求來下載圖片或其他二進位制檔案的程式,下載結果要按下載到的檔案型別進行存檔 將程式碼從伺服器的中獲取下來的程式碼,在我之前已經講過了這裡寫連結內容 這裡我們就直接將原始碼稍加改動,加入一個檔案並請將builder 寫入即可。 import

python3 用requests 保存網頁以及BeautifulSoup保存圖片並且在本地可以正常顯示文章的內容和圖片

-s bs4 圖片保存 ins date xml解析 站點 mkdir post 用requests 模塊做了個簡單的爬蟲小程序,將博客的一篇文章以及圖片保存到本地,文章格式存為‘.html‘。當文章保存到本地後,圖片的連接可能是目標站點的絕對或者相對路徑,所以要是想在本地

最簡單的方法壓縮圖片改變圖片大小

com post 分享 圖片大小 壓縮圖片 bubuko log OS 技術分享 1. 2. 3.按照百分比或像素 調整即可 最簡單的方法來壓縮圖片,改變圖片大小

文件通過svn updata更新不到並且svn st顯示被刪除的解決辦法

不顯示 什麽 文件丟失 eve 解決 lena 恢復 文件 依然 不知道什麽原因導致某些文件丟失,svn updata更新後仍然沒有找到,采用svn st 顯示這些文件被刪除,svn reslove 也解決不了,頭疼了很久,最近突然解決了,具體步驟如下(已經過驗證)

tableView 獲取網絡圖片並且設置為圓角(優化fps)

準備 程序 上下 web 介紹 null 方法 cli csb 代碼地址如下:<br>http://www.demodashi.com/demo/11088.html 一、準備工作 例子比較精簡,沒有什麽特殊要求,具備Xocde8.0左右版本的就好 二、程

前端之路:網頁內容轉圖片並且複製網頁內容並且優化體驗。(截圖外掛Clipboard.js 的使用)。

做專案遇到一個比較奇葩的需求,覺得很有意思,記錄下。方便以後回憶。 涉及框架(html2canvas.min.js   clipboard.mn.js): 直接連cdn <script src="https://cdn.bootcss.com/jquery/3.3

input圖片並且實現FileReader實圖片預覽效果

input img <img :src="upImg" alt=""> //屬性繫結為upImg在data()中聲名 <input class="lost" type="file" id="file" @change="upLoadImg($event)" ref="f

JS中通過指定大小壓縮圖片

前不久王二寫了一個圖片處理庫,可以指定圖片質量壓縮圖片,呼叫的是javaScript的原生方法 toDataURL 和 toBlob,庫裡有如下這些方法: 但是通過質量壓縮圖片有一些不足之處:無法確定壓縮後圖片的大小; 比如下圖,王二隨機選了三張圖片做測試(感興趣的小夥伴可以戳這裡自行測

通過cv2.resize()改變圖片大小

import cv2 import matplotlib.pyplot as plt  image=plt.imread('timg.jpg')res=cv2.resize(image,(224,224),interpolation=cv2.INTER_CUBIC) plt

VTK繪圖視窗vtkRenderWindow 儲存為圖片並且顯示繪圖視窗

VTK繪圖視窗vtkRenderWindow 儲存為圖片,並且不顯示繪圖視窗   參考連結; https://www.vtk.org/Wiki/VTK/Examples/Cxx/Visualization/CloseWindow https://www.vtk.org/Wi

美化combox 用法 —— 為其新增圖片改變顯示方式

對C# 中Combox控制元件的一些簡單的操作,改變其顯示特性,主要是改變其中的項的屬性 我們需要注意的是:每次為Combox新增項的時候,就會觸發DrawItem事件,而我們正是通過DrawItem來改變Combox的顯示特性的 using System;

通過點連線儲存圖片

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"><html>

spring mvc 中通過controller 傳遞物件給jsp並且資料繫結在修改值後回傳物件給controller

在controller 中需要指定 sessionAttribute的key @sessionattributes註解應用到Controller上面,可以將Model中的屬性同步到session當中。 當需要清除session當中的值得時候,我們只需要在

u-boot通過nfs從伺服器下載核心並且啟動核心,掛載根檔案系統的方法

http://www.linuxidc.com/Linux/2013-08/89154.htm http://blog.csdn.net/sinat_31500569/article/details/53120530 參考連結 首先要在電腦上安裝nfs伺服器 1.執行命令

laravel實現圖片並且製作縮圖按照日期存放

先上程式碼吧:前端程式碼:有很多小夥伴在做表單上傳檔案的時候沒有注意表單的上傳格式是什麼,就可能導致上傳檔案、圖片不成功!<form id="form1" name="form1" class="form-horizontal form-label-left" enct

美化combox 用法(二) 為其新增圖片改變顯示方式

對C# 中Combox控制元件的一些簡單的操作,改變其顯示特性,主要是改變其中的項的屬性 我們需要注意的是:每次為Combox新增項的時候,就會觸發DrawItem事件,而我們正是通過DrawItem

android LruCache技術非同步記載大量圖片並且有效解決OOM錯誤

這是一篇關於LruCache的應用例子,先上程式碼。 細節部分都加了註釋,歡迎交流。。 Images.java package com.example.testandroid; public class Images{ public final static Str

01.在vue中通過 JSONP 方式跨域

cas ech bsp import ret pre new res end 1 //1.引入 : 在main.js 中引入該文件即可 2 //2.使用: axios.jsonp(‘地址‘).then(res => { 3 //