sanji_pic
来源:互联网 发布:叉叉助手脚本提取源码 编辑:程序博客网 时间:2024/06/16 04:51
MainActivity
package com.example.a06_sanji_pic;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.ImageView;public class MainActivity extends AppCompatActivity { private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); } public void xianShi(View view) { new ImageHelper(MainActivity.this).displayImage(iv, "http://img.my.csdn.net/uploads/201407/26/1406383299_1976.jpg"); }}
-------------------------------------------------------------
ImageHelper
-------------------------------------------------------------
package com.example.a06_sanji_pic;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.AsyncTask;import android.os.Environment;import android.support.v4.util.LruCache;import android.widget.ImageView;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.security.MessageDigest;/** * Created by lenovo on 2017/9/9. */public class ImageHelper { Context context; private final LruCache<String, Bitmap> mCache; public ImageHelper(Context context) { this.context = context; int maxSize = (int) (Runtime.getRuntime().freeMemory() / 4); mCache = new LruCache<String, Bitmap>(maxSize) { @Override protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight(); } }; } public void displayImage(ImageView iv, String url) { Bitmap bitmap = mCache.get(url); if (bitmap != null) { iv.setImageBitmap(bitmap); return; } bitmap = loadFromLocal(url); if (bitmap != null) { iv.setImageBitmap(bitmap); return; } loadFromNet(iv, url); } private void loadFromNet(final ImageView iv, final String url) { AsyncTask<Void, Void, Bitmap> asyncTask = new AsyncTask<Void, Void, Bitmap>() { @Override protected Bitmap doInBackground(Void... voids) { try { URL path = new URL(url); HttpURLConnection connection = (HttpURLConnection) path.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); int responseCode = connection.getResponseCode(); if (responseCode == 200) { InputStream inputStream = connection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); return bitmap; } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Bitmap bitmap) { writeToSdCard(url, bitmap); mCache.put(url, bitmap); iv.setImageBitmap(bitmap); } }; asyncTask.execute(); } private void writeToSdCard(String url, Bitmap bitmap) { try { String path = encode(url); File file = new File(getSdCacheDir(), path); FileOutputStream fileOutputStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); fileOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } } private Bitmap loadFromLocal(String url) { try { String path = encode(url); String storageState = Environment.getExternalStorageState(); if (storageState.equals(Environment.MEDIA_MOUNTED)) { File file = new File(getSdCacheDir(), path); if (file.exists()) { Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); mCache.put(url, bitmap); return bitmap; } } } catch (Exception e) { e.printStackTrace(); } return null; } private File getSdCacheDir() { File dir; if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { dir = new File(Environment.getExternalStorageDirectory(), "/ANDroid/data/" + context.getPackageName() + "/pic"); if (!dir.exists()) { dir.mkdir(); } return dir; } return null; } private String encode(String string) throws Exception { byte[] hash = MessageDigest.getInstance("MD5").digest(string.getBytes("utf-8")); StringBuilder hex = new StringBuilder(hash.length * 2); for (byte b : hash) { if ((b & 0xFF) < 0x10) { hex.append("0"); } hex.append(Integer.toHexString(b & 0xFF)); } return hex.toString(); }}
阅读全文
0 0