Android 簡單的圖片查看器源碼實(shí)現(xiàn)
本文介紹了Android 簡單的圖片查看器源碼實(shí)現(xiàn),分享給大家,具體如下:
public class MainActivity extends Activity {
private EditText et_path;
private ImageView iv;
//創(chuàng)建handler 對象
// 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]獲取訪問圖片的路徑
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{
//第一次訪問 聯(lián)網(wǎng)獲取數(shù)據(jù)
System.out.println("第一次訪問連接網(wǎng)絡(luò)");
//[2.2]創(chuàng)建url對象
URL url = new URL(path);
//[2.3]獲取httpurlconnection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//[2.4]設(shè)置請求的方式
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]通過位圖工廠 獲取bitmap(bitmap)
final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
//這句api 不 管你在什么位置上調(diào)用 action都運(yùn)行在UI線程里
runOnUiThread(new Runnable() {
public void run() {
//run方法一定執(zhí)行在UI線程 里
// [2.9]把bitmap顯示到iv上
iv.setImageBitmap(bitmap);
}
});
// Message msg = Message.obtain(); //使用msg的靜態(tài)方法 可以減少對象的創(chuàng)建
// msg.obj = bitmap;
// handler.sendMessage(msg); //發(fā)消息
}
}
} catch (Exception e) {
e.printStackTrace();
}
};}.start();}}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android查看網(wǎng)絡(luò)圖片的實(shí)現(xiàn)方法
- Android圖片處理教程之全景查看效果實(shí)現(xiàn)
- Android仿百度圖片查看功能
- android自定義Camera拍照并查看圖片
- Android 通過網(wǎng)絡(luò)圖片路徑查看圖片實(shí)例詳解
- Android 網(wǎng)絡(luò)圖片查看器與網(wǎng)頁源碼查看器
- android網(wǎng)絡(luò)圖片查看器簡單實(shí)現(xiàn)代碼
- Android 實(shí)現(xiàn)WebView點(diǎn)擊圖片查看大圖列表及圖片保存功能
- Android實(shí)現(xiàn)圖片查看功能
相關(guān)文章
Android中實(shí)現(xiàn)Runnable接口簡單例子
這篇文章主要介紹了Android中實(shí)現(xiàn)Runnable接口簡單例子,著重點(diǎn)在如何實(shí)現(xiàn)run()方法,需要的朋友可以參考下2014-06-06
Android開發(fā)之a(chǎn)ctivity的生命周期詳解
這篇文章主要介紹了Android開發(fā)之a(chǎn)ctivity的生命周期,詳細(xì)分析了activity的運(yùn)行原理與生命周期,需要的朋友可以參考下2016-06-06
Android通過PHP服務(wù)器實(shí)現(xiàn)登錄功能
這篇文章主要為大家詳細(xì)介紹了Android通過PHP服務(wù)器實(shí)現(xiàn)登錄功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
android studio實(shí)現(xiàn)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了android studio實(shí)現(xiàn)計(jì)算器的具體方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android實(shí)現(xiàn)單頁面浮層可拖動view的一種方法
本篇文章主要介紹了Android實(shí)現(xiàn)單頁面浮層可拖動view的一種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
Android編程實(shí)現(xiàn)AlertDialog自定義彈出對話框的方法示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)AlertDialog自定義彈出對話框的方法,結(jié)合實(shí)例形式分析了Android AlertDialog自定義彈出對話框的基本功能與事件監(jiān)聽實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07
RxJava之網(wǎng)絡(luò)請求最常見的三種場景
本文想闡述一下當(dāng)你開發(fā)Android應(yīng)用并采用RxJava作為你的架構(gòu),尤其是有關(guān)網(wǎng)絡(luò)請求時(shí)最常見的三種場景。這篇文章主要介紹了RxJava之網(wǎng)絡(luò)請求最常見的三種場景,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android之淘寶商品列表長按遮罩效果的實(shí)現(xiàn)
這篇文章主要介紹了Android之淘寶商品列表長按遮罩效果的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05

