无法同时下载多个图像

时间:2014-10-12 07:23:28

标签: android android-asynctask

在我的程序中,我正在下载一个包含通知数据的Json数组文件。每个通知都包含一个地址字段,用于下载发件人图像。所以我运行另一个异步任务来下载每个json数组对象的图像。但是无论json数组有多少元素,都只下载第一个图像。我甚至尝试过executeOnExecutor,但只创建了文件夹而没有下载图像。

onpostexecute方法如下

      @Override
            protected void onPostExecute(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                super.onPostExecute(jsonObject);

                try {

                    if (jsonObject.getString("status").equalsIgnoreCase("true")) {
                        // refining the notices sent by server
                        JSONArray jsonArray = jsonObject.optJSONArray("notices");
                        for (int i = 0; i < jsonArray.length(); i++) {

                            JSONObject jsonArrayChild = jsonArray.getJSONObject(i);

                            // Retrieving data for each notice item
                            String name = jsonArrayChild.optString("name");
                            String heading = jsonArrayChild.optString("heading");
                            String date = jsonArrayChild.optString("date");
                            String noticeContent = jsonArrayChild
                                    .optString("content");
                            imageaddress = jsonArrayChild.optString("image");

                            imageName = imageaddress.substring(
                                    imageaddress.lastIndexOf("/") + 1,
                                    imageaddress.length());

                            // first we need to download the image from location
                            // specified in string imageaddress

                            new ProcessDownloadNoticeSenderImage()
                                    .execute(imageaddress);

                            // now inserting the overall data into database
                            // storing the local address of downloaded image in
                            // database
                            File file = new File(
                                    Environment.getExternalStorageDirectory()
                                            + "/veda/images/" + imageName);

                            String localImageAddress = file.getAbsolutePath();

                            // storing data in database
                            noticeMainDatabase.insertNotice(name, heading, date,
                                    noticeContent, localImageAddress);
                            // first we need to download the image from location
                            // specified in string imageaddress

                        }
               }
    }

和ProcessDownloadNoticeSenderImage如下所示

private class ProcessDownloadNoticeSenderImage extends
            AsyncTask<String, Integer, Bitmap> {

        Bitmap bitmap = null;

        @Override
        protected Bitmap doInBackground(String... params) {
            // TODO Auto-generated method stub
            File folder = new File(Environment.getExternalStorageDirectory()
                    + "/veda/images");

            boolean success = false;

            if (!folder.exists()) {
                success = folder.mkdir();
            }
            if (success) {
                Toast.makeText(
                        getApplicationContext(),
                        "veda/images folder is successfully created to store the images",
                        Toast.LENGTH_SHORT).show();
            }

            // saving the downloaded image into folder
            File f = new File(Environment.getExternalStorageDirectory(),
                    "/veda/images/" + imageName);
            if (f.exists()) {
                FileOutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        out.close();
                    } catch (Throwable ignore) {

                    }
                }
            } else if (!f.exists()) {
                FileOutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        out.close();
                    } catch (Throwable ignore) {

                    }
                }
            }

            try {
                bitmap = downloadImageFromServer(params[0]);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }

    }

帮助我如何下载多张图片,以便我可以在列表中显示它们。

0 个答案:

没有答案