日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

查看图片

發布時間:2024/9/5 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 查看图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

效果圖

界面中有三個控件,一個EditText,一個Button,一個ImageView。

1.activity_main布局代碼:

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context=".MainActivity" > 11 12 <ImageView 13 android:id="@+id/ivImage" 14 android:layout_width="match_parent" 15 android:layout_height="match_parent" 16 android:layout_weight="1" 17 android:background="@drawable/back_button" /> 18 19 <Button 20 android:id="@+id/btnView" 21 android:layout_width="143dp" 22 android:layout_height="wrap_content" 23 android:onClick="showImage" 24 android:paddingLeft="20px" 25 android:layout_marginLeft="90px" 26 android:text="瀏覽" /> 27 28 <EditText 29 android:id="@+id/etImageUrl" 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:ems="10" 33 android:hint="http://pica.nipic.com/2007-11-09/2007119124513598_2.jpg" 34 > 35 36 <requestFocus /> 37 </EditText> 38 39 </LinearLayout>

2.MainActivity代碼:

1 package com.example.chakan; 2 3 import android.os.Bundle; 4 import android.os.Handler; 5 import android.os.Looper; 6 import android.os.Message; 7 8 import android.app.Activity; 9 import android.graphics.Bitmap; 10 import android.graphics.BitmapFactory; 11 import android.text.TextUtils; 12 import android.util.Log; 13 import android.view.Menu; 14 import android.view.View; 15 import android.widget.Button; 16 import android.widget.EditText; 17 import android.widget.ImageView; 18 import android.widget.Toast; 19 public class MainActivity extends Activity { 20 private Button btn; 21 private EditText path; 22 private ImageView imgview; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 btn = (Button) findViewById(R.id.btnView); 29 path = (EditText) findViewById(R.id.etImageUrl); 30 imgview = (ImageView) findViewById(R.id.ivImage); 31 32 btn.setKeyListener(new OnClickListener() { 33 @Override 34 public void onClick(View v) { 35 Log.i("CLICK", ((Button) v).getText().toString()); 36 new Thread(runa).start(); 37 } 38 }); 39 } 40 41 public void setView() { 42 String picturepath = path.getText().toString(); 43 byte[] data = null; 44 try { 45 data = ImageService.getImage(picturepath); 46 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// BitmapFactory:圖片工廠! 47 Looper.prepare();// 必須調用此方法,要不然會報錯 48 Message msg = new Message(); 49 msg.what = 0; 50 msg.obj = bitmap; 51 handler.sendMessage(msg); 52 } catch (Exception e) { 53 Toast.makeText(getApplicationContext(), "獲取圖片錯誤", 1).show(); 54 } 55 } 56 57 private Handler handler = new Handler() { 58 @Override 59 public void handleMessage(Message msg) { 60 if (msg.what == 0) { 61 updateImageView((Bitmap) msg.obj); 62 } 63 } 64 65 }; 66 67 private Runnable runa = new Runnable() { 68 @Override 69 public void run() { 70 setView(); 71 } 72 }; 73 74 private void updateImageView(Bitmap bm) { 75 imgview.setImageBitmap(bm); 76 } 77 }

3.添加一個ImageService圖片服務類,里面包含一個獲取網絡數據的方法;

1 package com.example.chakan; 2 3 import java.io.InputStream; 4 import java.net.HttpURLConnection; 5 import java.net.URL; 6 7 public class ImageService { 8 9 // 獲取網絡圖片的數據 10 public static byte[] getImage(String picturepath) throws Exception { 11 URL url = new URL(picturepath); 12 HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 基于http協議的連接對象 13 conn.setConnectTimeout(10);// 10秒; 14 conn.setRequestMethod("GET");// 大寫 15 if (conn.getResponseCode() == 200) { 16 InputStream ins = conn.getInputStream(); 17 return StreamTool.read(ins); 18 } 19 return null; 20 } 21 }

4.添加一個流處理工作類StreamTool:

1 package com.example.chakan; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.InputStream; 5 6 public class StreamTool { 7 8 public static byte[] read(InputStream ins) throws Exception { 9 ByteArrayOutputStream outstream = new ByteArrayOutputStream(); 10 byte[] buffer = new byte[1024]; 11 int length = 0; 12 while ((length = ins.read(buffer)) > -1) { 13 outstream.write(buffer, 0, length); 14 } 15 outstream.close(); 16 return outstream.toByteArray(); 17 } 18 }

到此,程序已經完全編寫完畢。一定要注意在androidManifest中必須有配置文件,注意線程的使用。

轉載于:https://www.cnblogs.com/zhiyun930102/p/4618869.html

總結

以上是生活随笔為你收集整理的查看图片的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。