1

首先,我通过解析 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

而不是这个,我在另一个活动中显示相同的列表,我正在执行相同的过程。

还有其他两个活动:1.我使用相机意图捕获图像并上传到服务器(调用 api 并使用 Http Multipart Entity),以及 2.我从画廊上传图像。

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

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

4

1 回答 1

1

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

于 2013-01-03T09:15:16.940 回答