gpt4 book ai didi

java - 安卓图片缓存

转载 作者:可可西里 更新时间:2023-11-01 11:42:40 26 4
gpt4 key购买 nike

我有一个动态 View Flipper,它使用 250kb 的图像创建 View 。总 View 为 60。我收到内存不足错误。我将图像存储在可绘制文件夹中。我有一个延迟加载程序的示例代码,但它是从 url 加载的。我想更改功能以从 dawable 文件夹而不是远程 url 中获取。这是我的代码。任何人都可以帮忙吗?

private Bitmap getBitmap(String url)
{
File f=fileCache.getFile(url);

//from SD cache
//CHECK : if trying to decode file which not exist in cache return null
Bitmap b = decodeFile(f);
if(b!=null)
return b;

// Download image file from web
try {

Bitmap bitmap=null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();


// Constructs a new FileOutputStream that writes to file
// if file not exist then it will create file
OutputStream os = new FileOutputStream(f);

// See Utils class CopyStream method
// It will each pixel from input stream and
// write pixels to output stream (file)
Utils.CopyStream(is, os);

os.close();
conn.disconnect();

//Now file created and going to resize file with defined height
// Decodes image and scales it to reduce memory consumption
bitmap = decodeFile(f);

return bitmap;

} catch (Throwable ex){
ex.printStackTrace();
if(ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}


}
//Decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){

try {

//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
FileInputStream stream1=new FileInputStream(f);
BitmapFactory.decodeStream(stream1,null,o);
stream1.close();

//Find the correct scale value. It should be the power of 2.

// Set width/height of recreated image
final int REQUIRED_SIZE=85;

int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}

//decode with current scale values
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
FileInputStream stream2=new FileInputStream(f);
Bitmap bitmap=BitmapFactory.decodeStream(stream2, null, o2);
stream2.close();
return bitmap;

} catch (FileNotFoundException e) {
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}

最佳答案

你可以简单地使用 Picasso 库来处理这个,它会为你做一切

Picasso.with(context).load(R.drawable.drawableName).into(imageView);

在你的适配器中:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {

holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item, parent, false);
holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Picasso.with(context).load(R.drawable.drawableName).into(imageView);
return convertView;
}

class ViewHolder {
ImageView imageView;
}

链接:https://github.com/square/picasso

或者如果你还是不喜欢用 Picasso,让我们评论,我会分享一些类来修改图像,保存到缓存并加载,但是它太长了......

关于java - 安卓图片缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35646308/

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