Android 簡(jiǎn)單的圖片查看器源碼

public class MainActivity extends Activity {

private EditText et_path;
private ImageView iv;

      //創(chuàng)建handler 對(duì)象 
     // private Handler handler = new Handler(){
    //      
   //       //處理消息 
  //        public void handleMessage(android.os.Message msg) {
 //          
//          Bitmap bitmap =  (Bitmap) msg.obj;
//          iv.setImageBitmap(bitmap);  
//      };};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // [1]找到我們關(guān)心的控件

    et_path = (EditText) findViewById(R.id.et_path);
    iv = (ImageView) findViewById(R.id.iv);
    
    

}

// [2]點(diǎn)擊按鈕進(jìn)行查看 指定路徑的源碼
public void click(View v) {

    new Thread(){public void run() {
        try {
            //[2.1]獲取訪(fǎng)問(wèn)圖片的路徑 
            String path = et_path.getText().toString().trim();
            
             File  file = new File(getCacheDir(),Base64.encodeToString(path.getBytes(), Base64.DEFAULT));
             if (file.exists()&& file.length()>0) {
                //使用緩存 的圖片 
                 System.out.println("使用緩存圖片 ");
                 final Bitmap cacheBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                 //把cacheBitmap 顯示到iv上 
  //                     Message msg = Message.obtain();
  //                     msg.obj = cacheBitmap;
  //                     handler.sendMessage(msg); //發(fā)消息
                 
                 runOnUiThread(new Runnable() {
                    public void run() {
                    
                        iv.setImageBitmap(cacheBitmap);
                        
                    }
                });
                 
                 
            }else{
                //第一次訪(fǎng)問(wèn) 聯(lián)網(wǎng)獲取數(shù)據(jù) 
                System.out.println("第一次訪(fǎng)問(wèn)連接網(wǎng)絡(luò)");
            
            //[2.2]創(chuàng)建url對(duì)象 
            URL url = new URL(path);
            //[2.3]獲取httpurlconnection
             HttpURLConnection conn =  (HttpURLConnection) url.openConnection();
             
             //[2.4]設(shè)置請(qǐng)求的方式 
             conn.setRequestMethod("GET");
             //[2.5]設(shè)置超時(shí)時(shí)間
             conn.setConnectTimeout(5000);
             //[2.6]獲取服務(wù)器返回的狀態(tài)碼
             int code = conn.getResponseCode();
             if (code == 200) {
                //[2.7]獲取圖片的數(shù)據(jù)  不管是什么數(shù)據(jù)(txt文本 圖片數(shù)據(jù) )都是以流的形式返回 
                 InputStream in = conn.getInputStream();
                 
                 //[2.7]緩存圖片   谷歌給我們提供了一個(gè)緩存目錄
                
                 FileOutputStream fos = new FileOutputStream(file);
                 int  len = -1;
                 byte[] buffer = new byte[1024];//1kb
                 while((len=in.read(buffer))!=-1){
                     fos.write(buffer, 0, len);
                     
                 }
                 fos.close();
                 in.close();
                 
                 //[2.8]通過(guò)位圖工廠(chǎng) 獲取bitmap(bitmap)
                 final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

                 //這句api 不 管你在什么位置上調(diào)用 action都運(yùn)行在UI線(xiàn)程里 
                 runOnUiThread(new Runnable() {
                    public void run() {
                    
                        //run方法一定執(zhí)行在UI線(xiàn)程 里 
                        
  //              [2.9]把bitmap顯示到iv上 
                         iv.setImageBitmap(bitmap);
                        
                        
                    }
                });

// Message msg = Message.obtain(); //使用msg的靜態(tài)方法 可以減少對(duì)象的創(chuàng)建
// msg.obj = bitmap;
// handler.sendMessage(msg); //發(fā)消息

            }
        }
             
        } catch (Exception e) {
            e.printStackTrace();
        }
        };}.start();}}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容