gpt4 book ai didi

Android缩略图加载问题

转载 作者:行者123 更新时间:2023-11-30 04:53:00 26 4
gpt4 key购买 nike

我在我的项目中使用缩略图加载器,如下所述。问题是它会正确加载所有缩略图,除了大小约为 40K 的缩略图。当我们的后端提供时,不会生成那种缩略图,有时这最终也会导致崩溃。

我应该用它做什么?

public class ThumbnailManager  
{

private final Map<String, Bitmap> drawableMap;
public static Context context;
private Resources res;
private int thumbnail_size;

public ThumbnailManager()
{
drawableMap = new HashMap<String, Bitmap >();
res = new Resources(context.getAssets(), null, null);
thumbnail_size = res.getInteger(R.ThumbnailManager.THUMBNAIL_SIZE);
}

public Bitmap fetchBitmap(String urlString)
{
if(drawableMap.containsKey(urlString))
{
return (drawableMap.get(urlString));
}

//Log.d(getClass().getSimpleName(), " Image URL :: "+ urlString);
try
{
InputStream is = fetch(urlString);
android.util.Log.v("ThumbnailManager", "ThumbnailManager " + urlString);
drawableMap.put(urlString, BitmapFactory.decodeStream(is));//Bitmap.createScaledBitmap(BitmapFactory.decodeStream(is), thumbnail_size, thumbnail_size, false));
return drawableMap.get(urlString);
}
catch(Exception e)
{
android.util.Log.v("EXCEPTION", "EXCEPTION" + urlString);
return null;
}
}

public void fetchBitmapOnThread(final String urlString, final ImageView imageView)
{
if(drawableMap.containsKey(urlString))
{
imageView.setImageBitmap(drawableMap.get(urlString));
return;
}

if(urlString.compareTo("AUDIO") == 0)
{
Bitmap audioThumb = BitmapFactory.decodeResource(context.getResources(), R.drawable.timeline_audio_thumb);
drawableMap.put(urlString, Bitmap.createScaledBitmap(audioThumb, thumbnail_size, thumbnail_size, false));
imageView.setImageBitmap(drawableMap.get(urlString));
return;
}

final Handler handler = new Handler()
{
public void handleMessage(Message message)
{
imageView.setImageBitmap((Bitmap) message.obj);
}
};

Thread thread = new Thread()
{
public void run()
{
Bitmap urlBitmap = fetchBitmap(urlString);
Message message = handler.obtainMessage(1, urlBitmap);
handler.sendMessage(message);
}
};
thread.start();
}

public InputStream fetch(String urlString) throws IOException, MalformedURLException
{
final URL url = new URL(urlString);
final URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(true);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();

return(conn.getInputStream());
}
}

最佳答案

如果您在此处添加堆栈跟踪,那就太好了。但我怀疑它也是一个内存问题。如果您要将大量位图加载到内存中,它们需要的内存比原始文件大得多。前任。您的 40k 缩略图可能是 400k 作为位图,这取决于分辨率。使用 ddms 并观察您的应用程序占用了多少可用内存。如果它获得少于 2MB 的工作内存,则 BitmapFactory 很可能在解码您的大拇指时产生 RuntimeException。

关于Android缩略图加载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2477294/

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