布局就只有一个imageview,不贴了.
以下是mainactivity,只贴有关的部分,avd是安卓6.0
public class MainActivity extends AppCompatActivity
{
ImageView imageView;
File file; //cache的位置
DiskLruCache mDiskLruCache;
DiskLruCache.Editor editor;
URL imgurl;
HttpURLConnection con;
OutputStream editorOutput; //editor的输出流
InputStream in; //从网络获取的输入流
DiskLruCache.Snapshot snapshot;
Bitmap bitmap;
Handler handler;
//子线程已获取到bitmap时发消息给主线程
class MyHandler extends Handler
{
public void handleMessage(Message msg)
{
switch(msg.arg1)
{
case 1:
imageView.setImageBitmap(bitmap);
Log.e("this","已接受到消息");
}
}
}
//计算md5
public String hashKeyForDisk(String key)
{
String cacheKey;
try {
final MessageDigest mDigest = MessageDigest.getInstance("MD5");
mDigest.update(key.getBytes());
cacheKey = bytesToHexString(mDigest.digest());
} catch (NoSuchAlgorithmException e) {
cacheKey = String.valueOf(key.hashCode());
}
return cacheKey;
}
private String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
class myThread extends Thread
{
public void run() {
// String path = getExternalCacheDir().getPath();
String path=Environment.getExternalStorageDirectory().getPath();
file = new File(path + "/mytest");
if (!file.exists()) file.mkdirs();
try {
mDiskLruCache = DiskLruCache.open(file, 1, 1, 50 * 1024 * 1024);
String url = "http://v1.qzone.cc/skin/201310/08/17/19/5253ce06a29c2425.jpg%21600x600.jpg";
String key = hashKeyForDisk(url);
editor = mDiskLruCache.edit(key);
editorOutput = editor.newOutputStream(0);
//从网络获取数据流
imgurl = new URL(url);
con = (HttpURLConnection) imgurl.openConnection();
in = con.getInputStream();
int b;
//把输入流写到editor的输出流
while ((b = in.read()) != 0) editorOutput.write(b);
editorOutput.flush();
con.disconnect();
editor.commit();
mDiskLruCache.flush();
//从缓存中读取bitmap
snapshot = mDiskLruCache.get(key);
if (snapshot != null) {
FileInputStream is = (FileInputStream) snapshot.getInputStream(0);
bitmap = BitmapFactory.decodeStream(is);
Message msg = handler.obtainMessage();
msg.arg1 = 1;
handler.sendMessage(msg);
} else {
Log.e("this", "snapshot为空");
}
} catch (Exception e) {
Log.e("this", e.toString());
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int REQUEST_EXTERNAL_STORAGE=1;
String[] PERMISSIONS_STORAGE={Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
Log.e("this", "已拿到权限");
handler=new MyHandler();
imageView = (ImageView) MainActivity.this.findViewById(R.id.imageview);
Thread thread=new myThread();
thread.start();
}
}
下面再贴贴manifest中的权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
代码如上,运行效果如下
我试过直接从网络获取的流中直接获取bitmap是可以的,而这样先用DiskLruCache存了再取回来就不行,求大神指点指点