android decode bitmap,在Android中使用BitmapFactory.decodeStream()从缓存中加载图像

更新:即使我不从缓存中检索图像,我也试图通过Drawable进行检索,其中我将所有18个图像存储在“drawable-mdpi”文件夹中。仍然显示一个空白屏幕。

我能够从服务器检索图像并将图像(.GIF)保存到缓存中。但是,当我需要从缓存中加载该图像时,图像不会显示在屏幕上。这是做这项工作的代码:

File cacheDir = context.getCacheDir();

File cacheMap = new File(cacheDir, smallMapImageNames.get(i).toString());

if(cacheMap.exists()){

FileInputStream fis = null;

try {

fis = new FileInputStream(cacheMap);

Bitmap local = BitmapFactory.decodeStream(fis);

puzzle.add(local);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}else{

Drawable smallMap = LoadImageFromWebOperations(mapPiecesURL.get(i).toString());

if(i==0){

height1 = smallMap.getIntrinsicHeight();

width1 = smallMap.getIntrinsicWidth();

}

if (smallMap instanceof BitmapDrawable) {

Bitmap bitmap = ((BitmapDrawable)smallMap).getBitmap();

FileOutputStream fos = null;

try {

cacheMap.createNewFile();

fos = new FileOutputStream(cacheMap);

bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);

fos.flush();

fos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

puzzle.add(bitmap);

}

}ArrayList来存储图像名称:smallMapImageNames(图像名称也可以在URL中找到)

ArrayList存储图像的URL:mapPiecesURL

总结起来,我有2个问题

1)如何从缓存中加载图像?

2)关于bitmap.compress(),来自服务器的图像是.GIF格式,但是我应用了Bitmap.CompressFormat.PNG。那么这会有什么问题吗?

任何人都可以帮助我吗?

这两个功能

private Bitmap getBitMap(Context context) {

// TODO Auto-generated method stub

WifiPositioningServices wifiPositioningServices = new WifiPositioningServices();

String[] mapURLandCalibratedPoint1 = wifiPositioningServices.GetMapURLandCalibratedPoint("ERLab-1_1.GIF","ERLab"); //list of map pieces url in the first 9 pieces

String[] mapURLandCalibratedPoint2 = wifiPositioningServices.GetMapURLandCalibratedPoint("ERLab-4_1.GIF","ERLab"); //list of map pieces url in the last 9 pieces

ArrayList smallMapImageNames = new ArrayList();

ArrayList mapPiecesURL = new ArrayList();

for(int i=0; i

if(mapURLandCalibratedPoint1[i].length()>40){ //image url

int len = mapURLandCalibratedPoint1[i].length();

int subStrLen = len-13;

smallMapImageNames.add(mapURLandCalibratedPoint1[i].substring(subStrLen, len-3)+"JPEG");

mapPiecesURL.add(mapURLandCalibratedPoint1[i]);

}

else{

//perform other task

}

}

for(int i=0; i

if(mapURLandCalibratedPoint2[i].length()>40){ //image url

int len = mapURLandCalibratedPoint2[i].length();

int subStrLen = len-13;

smallMapImageNames.add(mapURLandCalibratedPoint2[i].substring(subStrLen, len-3)+"JPEG");

mapPiecesURL.add(mapURLandCalibratedPoint2[i]);

}

else{

//perform other task

}

}

Bitmap result = Bitmap.createBitmap(1029, 617, Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(result);

ArrayList puzzle = new ArrayList();

int height1 = 0 ;

int width1 = 0;

File cacheDir = context.getCacheDir();

for(int i=0; i<18; i++){

File cacheMap = new File(cacheDir, smallMapImageNames.get(i).toString());

if(cacheMap.exists()){

//retrieved from cached

try {

FileInputStream fis = new FileInputStream(cacheMap);

Bitmap bitmap = BitmapFactory.decodeStream(fis);

puzzle.add(bitmap);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}else{

//retrieve from server and cached it

Drawable smallMap = LoadImageFromWebOperations(mapPiecesURL.get(i).toString());

if(i==0){

height1 = smallMap.getIntrinsicHeight();

width1 = smallMap.getIntrinsicWidth();

}

if (smallMap instanceof BitmapDrawable) {

Bitmap bitmap = ((BitmapDrawable)smallMap).getBitmap();

FileOutputStream fos = null;

try {

cacheMap.createNewFile();

fos = new FileOutputStream(cacheMap);

bitmap.compress(CompressFormat.JPEG, 100, fos);

fos.flush();

fos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

puzzle.add(bitmap);

}

}

}

Rect srcRect;

Rect dstRect;

int cnt =0;

for (int j = 0; j < 3; j++) {

int newHeight = height1 * (j % 3);

for (int k = 0; k < 3; k++) {

if (j == 0 && k == 0) {

srcRect = new Rect(0, 0, width1, height1);

dstRect = new Rect(srcRect);

} else {

int newWidth = width1 * k;

srcRect = new Rect(0, 0, width1, height1);

dstRect = new Rect(srcRect);

dstRect.offset(newWidth, newHeight);

}

canvas.drawBitmap(puzzle.get(cnt), srcRect, dstRect,null);

cnt++;

}

}

for(int a=0; a<3; a++){

int newHeight = height1 * (a % 3);

for (int k = 3; k < 6; k++) {

if (a == 0 && k == 0) {

srcRect = new Rect(0, 0, width1*3, height1);

dstRect = new Rect(srcRect);

} else {

int newWidth = width1 * k;

srcRect = new Rect(0, 0, width1, height1);

dstRect = new Rect(srcRect);

dstRect.offset(newWidth, newHeight);

}

canvas.drawBitmap(puzzle.get(cnt), srcRect, dstRect,

null);

cnt++;

}

}

return result;

}

private Drawable LoadImageFromWebOperations(String url) {

// TODO Auto-generated method stub

try

{

InputStream is = (InputStream) new URL(url).getContent();

Drawable d = Drawable.createFromStream(is, "src name");

return d;

}catch (Exception e) {

System.out.println("Exc="+e);

return null;

}

}我实际上试图展示18张(3X6)图像来形成平面布置图。所以要显示图像,我使用两个for-loop来显示它。两个.GIF图像,ERLab-1_1.GIF和ERLab-4_1.GIF是每个组的中心部分。例如,第一行将是ERLab-0_0.GIF,ERLab-1_0.GIF,ERLab-2_0.GIF,ERLab-3_0.GIF,ERLab-4_0.GIF,ERLab-5_0.GIF。第二行将是第三行的XXX-X_1.GIF和XXX-X_2.GIF。

最后,

Bitmap resultMap = getBitMap(this.getContext());

bmLargeImage = Bitmap.createBitmap(1029 , 617, Bitmap.Config.ARGB_8888);

bmLargeImage = resultMap;然后在onDraw函数中将图像绘制到画布上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值