gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:19:50 28 4
gpt4 key购买 nike

更新:即使我没有从缓存中检索图像,我也尝试通过 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中找到)

存储图片URL的ArrayList:ma​​pPiecesURL

总而言之,我有两个问题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<String> smallMapImageNames = new ArrayList<String>();
ArrayList<String> mapPiecesURL = new ArrayList<String>();

for(int i=0; i<mapURLandCalibratedPoint1.length; 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<mapURLandCalibratedPoint2.length; 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<Bitmap> puzzle = new ArrayList<Bitmap>();

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 函数中将图像绘制到 Canvas 上。

最佳答案

我刚刚解决了我自己的问题。

在这一行中,canvas.drawBitmap(puzzle.get(cnt), srcRect, dstRect,null);在我使用它将位图绘制到 Canvas 上的每个 for 循环中,我需要将 ArrayList(拼图)中的每个项目转换为位图。只有这样,图像才会显示。

我认为如果 ArrayList 本身是确定的,ArrayList<Bitmap> puzzle = new ArrayList<Bitmap>(); ArrayList 中的每个项目都是位图类型。但这不总是如此吗?

关于android - 在 Android 中使用 BitmapFactory.decodeStream() 从缓存中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7543928/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com