---
title: "将保存的文件读为bitmap"
author: "googlelee"
date: "2012-05-21T10:38:56Z"
tags: []
categories: ["移动开发"]
slug: "bitmap"
draft: false
lastmod: "2012-05-21T10:38:56Z"
id: 1536072
url: "https://googlelee.iteye.com/blog/1536072"
---

private boolean getSavedPngAndSet(ImageView view, String logoUrl) {

String dir = this.getExternalCacheDir().getAbsolutePath();

File rootFileDir = new File(dir);

if(rootFileDir.exists() && rootFileDir.isDirectory()) {

 

        String filename = logoUrl.replace("/", "").replace(":", "").replace(",", "")

               .replace("\\\\", "").replace(".", "").replace("?", "").replace("|", "").replace("\\"", "")

               .replace("&gt;", "").replace("&lt;", "")+".png";

        

String\[] mPngList = rootFileDir.list();

 

for(int i=0; i&lt;mPngList.length; i++) {

       if(StringUtils.IsShowLog) {

        StringUtils.log(tag, "getSavePngList():mPngList\[i]="+mPngList\[i]);

       }

if(mPngList\[i].equals(filename)) {

try {

InputStream is = new BufferedInputStream(new FileInputStream(new File(dir+"/"+filename)));

Bitmap bitmap = BitmapFactory.decodeStream(is);

is.close();

view.setImageBitmap(bitmap);

getMyApplication().mUrlAndBitmap.put(logoUrl, bitmap);

       if(StringUtils.IsShowLog) {

        StringUtils.log(tag, "mPngList\[i].equals(filename)="+mPngList\[i].equals(filename));

       }

return true;

}catch (Exception e) {

e.printStackTrace();

return false;

}

}  

}

}

return false;

}