Android 網狀圖片查看器,

來源:互聯網
上載者:User

Android 網狀圖片查看器,

今天來實現一下android下的一款簡單的網狀圖片查看器

介面如下:

代碼如下:

<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=".MainActivity" >    <ImageView        android:id="@+id/iv"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_weight="1000" />    <EditText        android:singleLine="true"        android:text="http://img01.sogoucdn.com/app/a/100540002/1047586.jpg"        android:id="@+id/et_path"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:hint="請輸入圖片路徑" />    <Button        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:onClick="click"        android:text="瀏覽" /></LinearLayout>

代碼如下:

package com.wuyudong.imagesviewer;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import org.apache.http.HttpConnection;import android.os.Bundle;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.text.TextUtils;import android.view.Menu;import android.view.View;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {    private EditText et_path;    private ImageView iv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        et_path = (EditText) findViewById(R.id.et_path);        iv = (ImageView) findViewById(R.id.iv);    }    public void click(View view) {        String path = et_path.getText().toString().trim();        if (TextUtils.isEmpty(path)) {            Toast.makeText(this, "圖片路徑不可為空", 0).show();        } else {            // 串連伺服器get請求擷取圖片            try {                URL url = new URL(path);                // 根據url發送http的請求                HttpURLConnection conn = (HttpURLConnection) url                        .openConnection();                // 佈建要求的方式                conn.setRequestMethod("GET");                conn.setConnectTimeout(5000);                conn.setReadTimeout(5000);                conn.setRequestProperty(                        "User-Agent",                        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");                int code = conn.getResponseCode();                if (code == 200) {                    InputStream is = conn.getInputStream();                    Bitmap bitmap = BitmapFactory.decodeStream(is);                    iv.setImageBitmap(bitmap);                } else {                    Toast.makeText(this, "顯示圖片失敗", 0).show();                }            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();                Toast.makeText(this, "訪問擷取圖片失敗", 0).show();            }        }    }}

添加許可權:android.permission.INTERNET

運行後報錯:android.os.NetworkOnMainThreadException

解釋一下,從Honeycomb SDK(3.0)開始,google不再允許網路請求(HTTP、Socket)等相關操作直接在Main Thread類中,其實本來就不應該這樣做,直接在UI線程進行網路操作,會阻塞UI、使用者體驗相當差!

所以,在Honeycomb SDK(3.0)以下的版本,你還可以繼續在Main Thread裡這樣做,在3.0以上,就不行了

具體解決辦法見後文

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.