gpt4 book ai didi

android - java.lang.OutOfMemoryError : bitmap size exceeds VM budget on line Drawable. createFromStream(((java.io.InputStream) new URL(url), "") 在android中

转载 作者:行者123 更新时间:2023-11-30 03:51:06 24 4
gpt4 key购买 nike

首先我通过解析 json 响应创建一个图像和图像信息的 Arraylist

`JSONArray jsonArray = jsonObj.getJSONArray("result");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObjInner = jsonArray.getJSONObject(i);

ImageInfo imageInfo = new ImageInfo();
imageInfo.setImageUrl("http://www.dvimaytech.com/markphoto/" + jsonObjInner.getString("image"));

new GetDrawableFromUrl(imageInfo).execute();

imageInfo.setEmail(jsonObjInner.getString("emailid"));
imageInfo.setImage_id(jsonObjInner.getString("image_id"));
imageInfo.setImage_name(jsonObjInner.getString("image_name"));
imageInfo.setAmount(jsonObjInner.getString("amount"));
imageInfo.setImage_description(jsonObjInner.getString("image_description"));
imageInfo.setDate_created(jsonObjInner.getString("date_created"));
}`

然后我使用代码在异步任务中从另一个服务器获取图像

public class GetDrawableFromUrl extends AsyncTask<Void, Void, Drawable> {
public ImageInfo imageInfoObj;

GetDrawableFromUrl(ImageInfo imageInfo) {
imageInfoObj = imageInfo;
}

@Override
protected Drawable doInBackground(Void... params) {
try {
return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(imageInfoObj.getImageUrl()).getContent()), "src_name");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

protected void onPostExecute(Drawable drawable) {
if (drawable != null) {
imageInfoObj.setImage(drawable);
listImageInfo.add(imageInfoObj);
showImagesInList();
}
}
}

我的适配器类代码

public class MyImageListAdapter extends BaseAdapter {

private LayoutInflater inflater = null;

public MyImageListAdapter() {
inflater = (LayoutInflater) HomeActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
return listImageInfo.size();
}

public Object getItem(int position) {
return listImageInfo.get(position);
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.list_row, null);
}

TextView tvName = (TextView) vi.findViewById(R.id.tv_name);
TextView tvTime = (TextView) vi.findViewById(R.id.tv_time);
ImageView image = (ImageView) vi.findViewById(R.id.iv_image);
final Button btnDelete = (Button) vi.findViewById(R.id.btn_delete);

image.setImageDrawable(listImageInfo.get(position).getImage());

tvName.setText("Name: " + listImageInfo.get(position).getImage_name());
tvTime.setText("Date: " + listImageInfo.get(position).getDate_created());

btnDelete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final int position = listView.getPositionForView((View) v.getParent());
positionOgBtnToDelete = position;
Log.v("delete btn clicked", "delete btn no: " + position);
Toast.makeText(HomeActivity.this, "Btn delete position: " + position, Toast.LENGTH_LONG).show();
showAlertToConfirmDelete();
}
});
return vi;
}
}

现在我的列表只包含 5-6 张图片。但是我在线上收到错误 OutOfMemory 错误

return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(imageInfoObj.getImageUrl()).getContent()), "src_name");

我的日志-猫是

 E/dalvikvm-heap(10927): 7680000-byte external allocation too large for this process.
: D/dalvikvm(10927): GC_FOR_MALLOC freed 0K, 55% free 3286K/7303K, external 54717K/56765K, paused 32ms
: E/GraphicsJNI(10927): VM won't let us allocate 7680000 bytes
: D/skia(10927): libjpeg error 105 < Ss=%d, Se=%d, Ah=%d, Al=%d> from allocPixelRef [1600 1200]
D/skia(10927): --- decoder->decode returned false
W/dalvikvm(10927): threadid=9: thread exiting with uncaught exception (group=0x40018578)
E/AndroidRuntime(10927): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(10927): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(10927): at android.os.AsyncTask$3.done(AsyncTask.java:200)
E/AndroidRuntime(10927): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
E/AndroidRuntime(10927): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
E/AndroidRuntime(10927): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
E/AndroidRuntime(10927): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
E/AndroidRuntime(10927): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
E/AndroidRuntime(10927): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
E/AndroidRuntime(10927): at java.lang.Thread.run(Thread.java:1019)
E/AndroidRuntime(10927): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
E/AndroidRuntime(10927): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
E/AndroidRuntime(10927): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:573)
E/AndroidRuntime(10927): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
E/AndroidRuntime(10927): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
E/AndroidRuntime(10927): at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
E/AndroidRuntime(10927): at com.markphoto_activities.DisplayImagesActivity$GetDrawableFromUrl.doInBackground(DisplayImagesActivity.java:257)
E/AndroidRuntime(10927): at com.markphoto_activities.DisplayImagesActivity$GetDrawableFromUrl.doInBackground(DisplayImagesActivity.java:1)
E/AndroidRuntime(10927): at android.os.AsyncTask$2.call(AsyncTask.java:185)
E/AndroidRuntime(10927): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
E/AndroidRuntime(10927): ... 4 more
D/dalvikvm(10927): GC_EXTERNAL_ALLOC freed 122K, 55% free 3329K/7303K, external 55034K/56765K, paused 49ms
E/dalvikvm-heap(10927): 4194304-byte external allocation too large for this process.
D/dalvikvm(10927): GC_FOR_MALLOC freed <1K, 55% free 3329K/7303K, external 55034K/56765K, paused 32ms
E/GraphicsJNI(10927): VM won't let us allocate 4194304 bytes

取而代之的是,我在另一个 Activity 中显示相同的列表,我在其中执行相同的过程。

还有另外两个 Activity 1.我使用相机 Intent 捕获图像并上传到服务器(调用api并使用Http Multipart Entity),2. 我从画廊上传图片。

这不是我遇到此错误的唯一地方,实际上在其他地方也有很多。但是 90% 的错误都出现在这条线上。

我想知道,问题是否是因为所有图像的单个大尺寸图像一起占用了更多内存。以及如何删除它?

最佳答案

使用图像加载器你可以找到一个例子 here

关于android - java.lang.OutOfMemoryError : bitmap size exceeds VM budget on line Drawable. createFromStream(((java.io.InputStream) new URL(url), "") 在android中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14135091/

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