當前位置:首頁 » 安卓系統 » android判斷圖片

android判斷圖片

發布時間: 2025-05-17 20:32:33

『壹』 Android 怎麼查看網路圖片的方法

隨著智能手機的普及,尤其是基於安卓系統的廣泛使用,使android開發具有很好的前景.下面分享一個Android 怎麼查看網路圖片的方法. 第一步:下面是具體布局文件 android:id="@+id/picturepagh" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world" / android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="查看" / android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="200px" / 第二步:在MainActivity中進行圖片圖示代碼編寫 public class MainActivity extends Activity { private Button btn; private EditText path; private ImageView imgview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); path = (EditText) findViewById(R.id.picturepagh); imgview = (ImageView) findViewById(R.id.imageView); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.i("CLICK", ((Button) v).getText().toString()); new Thread(runa).start(); } }); } public void setView() { String picturepath = path.getText().toString(); byte[] data = null; try { data = ImageService.getImage(picturepath); Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// BitmapFactory:圖片工廠! Looper.prepare();// 必須調用此方法,要不然會報錯 Message msg = new Message(); msg.what = 0; msg.obj = bitmap; handler.sendMessage(msg); } catch (Exception e) { Toast.makeText(getApplicationContext(), "獲取圖片錯誤", 1).show(); } } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0) { updateImageView((Bitmap) msg.obj); } } }; private Runnable runa = new Runnable() { @Override public void run() { setView(); } }; private void updateImageView(Bitmap bm) { imgview.setImageBitmap(bm); } } 第三步,添加一個流處理工作類StreamTool public class StreamTool { public static byte[] read(InputStream ins) throws Exception { ByteArrayOutputStream outstream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length = 0; while ((length = ins.read(buffer)) -1) { outstream.write(buffer, 0, length); } outstream.close(); return outstream.toByteArray(); } } 第四步、添加一個ImageService圖片服務類,裡麵包含一個獲取網路數據的方法; public class ImageService { // 獲取網路圖片的數據 public static byte[] getImage(String picturepath) throws Exception { URL url = new URL(picturepath); HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 基於http協議的連接對象 conn.setConnectTimeout(10);// 10秒; conn.setRequestMethod("GET");// 大寫 if (conn.getResponseCode() == 200) { InputStream ins = conn.getInputStream(); return StreamTool.read(ins); } return null; } }    Instagram APP

熱點內容
matlab文件存儲 發布:2025-07-05 10:40:46 瀏覽:82
梅州市用工實名制管理平台雲存儲 發布:2025-07-05 10:28:59 瀏覽:75
安卓origin怎麼設置 發布:2025-07-05 10:20:10 瀏覽:539
安卓為什麼跳水 發布:2025-07-05 09:55:08 瀏覽:87
達內學校php 發布:2025-07-05 09:52:05 瀏覽:398
獲取資料庫所有表 發布:2025-07-05 09:39:12 瀏覽:654
wcfphp 發布:2025-07-05 09:39:07 瀏覽:178
解壓密碼對 發布:2025-07-05 09:33:00 瀏覽:586
廣東金稅盤的伺服器地址是什麼 發布:2025-07-05 09:10:29 瀏覽:705
掛式手機卡的服務密碼是多少 發布:2025-07-05 08:57:40 瀏覽:945