Android擷取網狀圖片應用樣本

來源:互聯網
上載者:User

1、養成好習慣,配置字串資源檔 strings.xml


 

 xml version="1.0" encoding="utf-8"?> <resources>     <string name="app_name">網狀圖片查看器</string>     <string name="action_settings">Settings</string>     <string name="hello_world">Hello world!</string>     <string name="imgpath">輸入圖片地址:</string>     <string name="getBtn">擷取圖片</string>     <string name="error">擷取圖片失敗</string> </resources> <?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">網狀圖片查看器</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string> <string name="imgpath">輸入圖片地址:</string> <string name="getBtn">擷取圖片</string> <string name="error">擷取圖片失敗</string></resources>

2、布局檔案,使用垂直布局


 

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context=".MainActivity" >      <TextView          android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/imgpath"         />          <EditText android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:id="@+id/imgpathInput"         android:text="http://avatar.csdn.net/B/E/7/1_gaotong2055.jpg"         android:inputType="text" />     <Button           android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/getBtn"         android:id="@+id/getBtn"         />     <ImageView          android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/imgView"         /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/imgpath"        />     <EditText android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/imgpathInput"        android:text="http://avatar.csdn.net/B/E/7/1_gaotong2055.jpg"        android:inputType="text" />    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/getBtn"        android:id="@+id/getBtn"        />    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/imgView"        /></LinearLayout>

、編寫代碼

這裡為了方便看代碼,都寫在一個類裡面了。

可以把裡面的靜態方法單獨拆分出來,寫在一個工具類中,結構更好。


 

public class MainActivity extends Activity implements OnClickListener {     private EditText pathText;     private ImageView imageView;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         pathText = (EditText) this.findViewById(R.id.imgpathInput);         imageView = (ImageView) this.findViewById(R.id.imgView);         Button button = (Button) this.findViewById(R.id.getBtn);         button.setOnClickListener(this);     }      @Override     public boolean onCreateOptionsMenu(Menu menu) {         getMenuInflater().inflate(R.menu.main, menu);         return true;     }      @Override     public void onClick(View v) {         String path = pathText.getText().toString();         byte[] data = null;         try {             data = getImgData(path);         } catch (Exception e) {             e.printStackTrace();             Toast.makeText(this, R.string.error, 1).show();         }         Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);         imageView.setImageBitmap(bitmap);     }      public static byte[] getImgData(String path) throws Exception {          URL url = new URL(path);         HttpURLConnection conn = (HttpURLConnection) url.openConnection();         conn.setConnectTimeout(5000);// 逾時時間5秒          conn.setRequestMethod("GET");         if (conn.getResponseCode() == 200) {             InputStream in = conn.getInputStream();             return read(in);         } else {             Log.d("tong.getImg", "伺服器無響應");         }          return null;     }      /**     * 從一個輸入資料流中讀取資料,並返回     *      * @param in     * @return byte[] 資料     * @throws IOException     */     public static byte[] read(InputStream in) throws IOException {         // 開闢一個記憶體的地區,以寫入資料          ByteArrayOutputStream outStream = new ByteArrayOutputStream();         byte[] buffer = new byte[10240];         int len = 0;         while ((len = in.read(buffer)) != -1) {             outStream.write(buffer, 0, len);         }         outStream.close();          return outStream.toByteArray(); // 返回記憶體中的資料      }  } public class MainActivity extends Activity implements OnClickListener { private EditText pathText; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  pathText = (EditText) this.findViewById(R.id.imgpathInput);  imageView = (ImageView) this.findViewById(R.id.imgView);  Button button = (Button) this.findViewById(R.id.getBtn);  button.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) {  getMenuInflater().inflate(R.menu.main, menu);  return true; } @Override public void onClick(View v) {  String path = pathText.getText().toString();  byte[] data = null;  try {   data = getImgData(path);  } catch (Exception e) {   e.printStackTrace();   Toast.makeText(this, R.string.error, 1).show();  }  Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  imageView.setImageBitmap(bitmap); } public static byte[] getImgData(String path) throws Exception {  URL url = new URL(path);  HttpURLConnection conn = (HttpURLConnection) url.openConnection();  conn.setConnectTimeout(5000);// 逾時時間5秒  conn.setRequestMethod("GET");  if (conn.getResponseCode() == 200) {   InputStream in = conn.getInputStream();   return read(in);  } else {   Log.d("tong.getImg", "伺服器無響應");  }  return null; } /**  * 從一個輸入資料流中讀取資料,並返回  *  * @param in  * @return byte[] 資料  * @throws IOException  */ public static byte[] read(InputStream in) throws IOException {  // 開闢一個記憶體的地區,以寫入資料  ByteArrayOutputStream outStream = new ByteArrayOutputStream();  byte[] buffer = new byte[10240];  int len = 0;  while ((len = in.read(buffer)) != -1) {   outStream.write(buffer, 0, len);  }  outStream.close();  return outStream.toByteArray(); // 返回記憶體中的資料 }}

 

相關文章

聯繫我們

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