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中操作SQLite數(shù)據(jù)庫快速入門教程
這篇文章主要介紹了Android中操作SQLite數(shù)據(jù)庫快速入門教程,本文講解了數(shù)據(jù)庫基礎(chǔ)概念、Android平臺下數(shù)據(jù)庫相關(guān)類、創(chuàng)建數(shù)據(jù)庫、向表格中添加數(shù)據(jù)、從表格中查詢記錄等內(nèi)容,需要的朋友可以參考下2015-03-03
Android動(dòng)態(tài)模糊效果的快速實(shí)現(xiàn)方法
這篇文章主要介紹了Android動(dòng)態(tài)模糊效果的快速實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android 自定義view和屬性動(dòng)畫實(shí)現(xiàn)充電進(jìn)度條效果
近期項(xiàng)目中需要使用到一種類似手機(jī)電池充電進(jìn)度的動(dòng)畫效果,以前沒學(xué)屬性動(dòng)畫的時(shí)候,是用圖片+定時(shí)器的方式來完成的,下面給大家分享android自定義view和屬性動(dòng)畫實(shí)現(xiàn)充電進(jìn)度條2016-12-12
Android編程實(shí)現(xiàn)自定義手勢的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義手勢的方法,結(jié)合實(shí)例形式分析了Android自定義手勢的功能、相關(guān)函數(shù)與具體實(shí)現(xiàn)步驟,需要的朋友可以參考下2016-10-10
Android 搜索結(jié)果匹配關(guān)鍵字且高亮顯示功能
這篇文章主要介紹了Android 搜索結(jié)果匹配關(guān)鍵字且高亮顯示功能,需要的朋友可以參考下2017-05-05
flutter優(yōu)雅實(shí)現(xiàn)掃碼槍獲取數(shù)據(jù)源示例詳解
這篇文章主要為大家介紹了flutter優(yōu)雅實(shí)現(xiàn)掃碼槍獲取數(shù)據(jù)源示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
ListView實(shí)現(xiàn)聊天列表之處理不同數(shù)據(jù)項(xiàng)
這篇文章主要為大家詳細(xì)介紹了ListView實(shí)現(xiàn)聊天列表之處理不同數(shù)據(jù)項(xiàng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
創(chuàng)建Android庫的方法及Android .aar文件用法小結(jié)
本文給大家介紹了創(chuàng)建Android庫的方法及Android中 .aar文件生成方法與用法詳解,涉及到創(chuàng)建庫模塊操作步驟及開發(fā)注意事項(xiàng),需要的朋友參考下吧2017-12-12
Android中ActionBar和ToolBar添加返回箭頭的實(shí)例代碼
這篇文章主要介紹了Android中ActionBar和ToolBar添加返回箭頭的實(shí)例代碼,需要的朋友可以參考下2017-09-09

