- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有一个问题,因为内存不足,我的应用程序崩溃了。
首先我的适配器代码确实有很多好的解决方案:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.accidentally_open_internet_explorer,
R.drawable.angry_shaking, R.drawable.are_you_fucking_kidding_me,
R.drawable.angry_with_pc,
R.drawable.sample_7, R.drawable.awkward_moment,
R.drawable.awkward_moment,
R.drawable.beer_guy,
R.drawable.beng,
R.drawable.accidentally_open_internet_explorer, R.drawable.boobs,
R.drawable.big_smile, R.drawable.cereal_guy,
R.drawable.challenge_accepted_drunk, R.drawable.challenge_accepted,
R.drawable.bad_pokerface, R.drawable.challenge_considered,
R.drawable.challenge_denied, R.drawable.challenge_failed,
R.drawable.classic_rage, R.drawable.one_does_not_simply,
R.drawable.computer_stare, R.drawable.computer_slice,
R.drawable.concentrated, R.drawable.concentrate,
R.drawable.concentrated_teeth, R.drawable.cry_pc,
R.drawable.dat_ass, R.drawable.desk_flip,
R.drawable.double_facepalm, R.drawable.duck_yeah,
R.drawable.fap, R.drawable.fap_accepted,
R.drawable.fap_gentleman, R.drawable.feels_good_man,
R.drawable.forever_a_gamer, R.drawable.forever_alone_clean,
R.drawable.forever_alone_sad, R.drawable.forever_alone_together,
R.drawable.fuck_that_bitch_yao_pff, R.drawable.fuck_that_yao_ming,
R.drawable.fuck_yeah, R.drawable.fuck_yeah_close_enough,
R.drawable.gentleman_troll, R.drawable.happy_smoking,
R.drawable.haters_gonna_hate, R.drawable.if_you_know_what_i_mean_mr_bean,
R.drawable.if_you_know_what_i_mean_mr_bean_blank, R.drawable.if_you_know_what_i_mean_mr_bean_blank,
R.drawable.impossibru, R.drawable.indeed,
R.drawable.jesus, R.drawable.keyboard_break,
R.drawable.knowthatfeel, R.drawable.like_a_sir,
R.drawable.long_long_neck_surprise, R.drawable.look_from_bottom,
R.drawable.me_gusta, R.drawable.me_gusta_creepy,
R.drawable.me_gusta_makeup, R.drawable.not_sure_if_gusta,
R.drawable.now_kiss, R.drawable.one_does_not_simply,
R.drawable.sample_1, R.drawable.sample_4,
R.drawable.sample_7, R.drawable.sample_12,
R.drawable.sample_13, R.drawable.today_is_monday,
R.drawable.trees_smile, R.drawable.trees_stoned,
R.drawable.trees_stoned_exhale, R.drawable.trees_stoned_inhale,
R.drawable.wonka, R.drawable.y_u_no,
R.drawable.yea_science, R.drawable.you_are_the_man
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
.....
First:
public View getView(int position, View convertView, ViewGroup parent) {
//This actually is a bad solution, because every time convertView is reused, you will still initialize new ImageView, which is wrong
//ImageView imageView = new ImageView(this.mContext);
//new BitmapWorkerTask(imageView).execute(Tattoos[position]);
//return imageView;
//Better solution
ImageView imageView = null;
if (convertView == null) {
imageView = new ImageView(this.mContext);
new BitmapWorkerTask(imageView).execute(Tattoos[position]);
//create new ImageView if it is not present and populate it with some image
} else {
imageView = (ImageView) convertView;
//re-use ImageView that already exists in memory
}
return imageView;
}
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private int data = 0;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
return decodeSampledBitmapFromResource(ImageAdapter.this.mContext.getResources(), data, 100, 100);
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(100, 70));
}
}
}
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
// TextView txt = new TextView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(
new GridView.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
Bitmap m_d = BitmapFactory.decodeResource(mContext.getResources(),
mThumbIds[position]);
if (m_d != null)
{
Bitmap resizedBitmap = Bitmap.createScaledBitmap(m_d, 205, 205, true);
imageView.setImageBitmap(resizedBitmap);
};
return imageView;}}
最佳答案
每当使用BitmapFactory创建位图时,必须确保在完成该位图实例后调用.recycle()
,否则很快就会耗尽内存。
您的第一个解决方案更接近正确,因为您需要回收那些convertView对象,因此只需确保您的convertView存在,并且在创建新的bitmap之前,您已经回收了它可能已经保留的所有位图。
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
if (convertView == null) {
imageView = new ImageView(this.mContext);
} else {
imageView = (ImageView) convertView;
}
// clean up your old bitmap first, if there is one.
if(imageView.getDrawable() instanceof BitmapDrawable){
BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
imageView.setDrawable(null);
if(bd.getBitmap() != null){
bd.getBitmap().recycle();
}
bd = null;
}
new BitmapWorkerTask(imageView).execute(Tattoos[position]);
return imageView;
}
关于java - GridView内存不足位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22693590/
所以我正在为考试复习,并在 SQL 河(或荒地)中撞到了一块大石头 我制作了以下表格并插入了以下数据: create table Permissions ( fileName VARCHAR(
我有一个使用 maxWidth 定义的 jqueryui 对话框。 $("#myDialog").dialog({ autoOpen: false, width: 'a
注意:我遗漏了不相关的代码 所以我目前正在研究 CCC 1996 P1,这个问题的全部目的是能够计算一个整数输入是完美数、不足数还是充数。我上面列出的代码可以工作,但是我认为它太慢了。该代码会迭代每个
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我正在使用 Go 和 Redis 开发 API。问题是RAM使用不足,我找不到问题的根源。 TL;DR 版本 有数百/数千个哈希对象。每个 1 KB 的对象(键+值)占用大约 0.5 MB 的 RAM
在我的 GCE Kubernetes 集群上,我无法再创建 pod。 Warning FailedScheduling pod (www.caveconditions.com-f1be467e3
当我尝试在EKS Fargate群集上安装指标服务器时,它抛出错误: 0/4 nodes are available: 4 Insufficient pods. 按照以下说明从此处安装指标服务器:ht
遍布this document Apple 提到 iOS 在某些情况下会终止应用程序,最常见的原因似乎是释放一些 RAM。这会导致未实现状态恢复的应用程序出现问题——用户正在处理和暂时离开的一些内容可
尝试处理一个10分钟的音频文件时出现以下错误。我刚刚开始使用Google Cloud产品,所以我是唯一访问此资源的人。我怎么可能超出配额?配额设置为其默认值,我认为我没有任何限制。还有其他原因吗? 我
R 语言让我感到困惑。实体有模式和类,但即使这样也不足以完全描述实体。 这个answer说 In R every 'object' has a mode and a class. 所以我做了这些实验:
我在 west-1 有一个 Openshift v3 项目。在其中,我有一个运行良好的应用程序,但在 GitHub 提交代码中非常下游的内容后,该应用程序停止工作。问题在于制作 pod: No nod
我在 west-1 有一个 Openshift v3 项目。在其中,我有一个运行良好的应用程序,但在 GitHub 提交代码中非常下游的内容后,该应用程序停止工作。问题在于制作 pod: No nod
在 how-do-i-access-the-stackoverflow-api-from-mathematica我概述了如何使用 SO API 让 Mathematica 制作一些有趣的顶级回答者声誉
所以在 GKE 上,我有一个 Node.js app,每个 pod 使用大约:CPU(cores): 5m, MEMORY: 100Mi 但是我只能为每个 Node 部署 1 个 pod。我使用的是
我正在使用 async.eachOfSeries 超过 300 个数组并请求一些 GA api,它工作正常但有时我会收到错误.. UnhandledPromiseRejectionWarning:错误
我正在尝试在 AWS ec2 上托管的 kubernetes 集群上使用 mr3 设置配置单元。当我运行命令 run-hive.sh 时,Hive 服务器启动,并且 master-DAg 被初始化,但
创建订阅时有时会出现以下错误: Insufficient tokens for quota 'administrator' and limit 'CLIENT_PROJECT-100s' of ser
我是一名优秀的程序员,十分优秀!