0

我正在从 parse.com下载图像。我永远不知道我将拥有多少图像,所以我使用计数器来检索它们。出于某种原因,我的计数器变为 5,并下载了 5 次图像。在我的存储中,我只有一张图像。这是我的代码:

String root = Environment.getExternalStorageDirectory().toString();
int i = 0;

检索图像:

ParseQuery<ParseObject> query = ParseQuery.getQuery("fightGallery");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> parseObjects, com.parse.ParseException e) {
            if (e == null) {

                int size = parseObjects.size();
                Log.d("query size", "size is " + size + " int i is " + i);
                while (i < size) {
                    ParseFile fileObject = parseObjects.get(i).getParseFile("image");

                    fileObject.getDataInBackground(new GetDataCallback() {
                        @Override
                        public void done(byte[] bytes, ParseException e) {
                            if (e == null) {
                                Log.d("Data", "We have data successfully " +i);

                                Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                                saveImagesInSdCard(bmp, i);


                            } else {
                                Log.d("ERROR: ", "" + e.getMessage());
                                progressDialog.dismiss();
                            }
                        }
                    });
                    i++;
                }
            } else {
                Log.d("ERROR:", "" + e.getMessage());
                progressDialog.dismiss();
            }
        }
    });

这是我的saveImagesInSdCard()

 private void saveImagesInSdCard(Bitmap bmp, int i) {


    File myDir = new File(root + "/clash_images");
    myDir.mkdirs();
    String fname = "Image";
    File file = new File(myDir, fname+i+".jpg");
   // if (file.exists()) file.delete();
    Log.d("IMAGE", "SAVED " +fname + i);

    try {
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.flush();
        out.close();

    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e2) {
        e2.printStackTrace();
    }


}

这是我的 logcat,显示了我所有的Log.d

08-07 12:54:45.586  10403-10403/com.codealchemist.clashmma D/query size: size is 5 int i is 0
08-07 12:54:46.407  10403-10403/com.codealchemist.clashmma D/Data: We have data successfully 5
08-07 12:54:46.527  10403-10403/com.codealchemist.clashmma D/IMAGE: SAVED Image5
08-07 12:54:48.079  10403-10403/com.codealchemist.clashmma I/Choreographer: Skipped 100 frames!  The application may be doing too much work on its main thread.
08-07 12:54:48.089  10403-10403/com.codealchemist.clashmma D/Data: We have data successfully 5
08-07 12:54:48.199  10403-10405/com.codealchemist.clashmma D/dalvikvm: GC_CONCURRENT freed 8524K, 20% free 38799K/48391K, paused 13ms+4ms, total 60ms
08-07 12:54:48.199  10403-10403/com.codealchemist.clashmma D/IMAGE: SAVED Image5
08-07 12:54:50.131  10403-10403/com.codealchemist.clashmma D/Data: We have data successfully 5
08-07 12:54:50.181  10403-10403/com.codealchemist.clashmma D/IMAGE: SAVED Image5
08-07 12:54:51.242  10403-10403/com.codealchemist.clashmma D/Data: We have data successfully 5
08-07 12:54:51.312  10403-10403/com.codealchemist.clashmma D/IMAGE: SAVED Image5
08-07 12:54:53.494  10403-10403/com.codealchemist.clashmma I/Choreographer: Skipped 323 frames!  The application may be doing too much work on its main thread.

如您所见,在我的 logcat 中,我的i从未从 5 更改。我如何构建它以下载所有 5 个图像?

4

2 回答 2

1

当你GetDataCallback在下载文件后执行时,循环已经运行了 5 次,因此i每次的值都是 5。

于 2013-08-07T19:43:37.077 回答
0

我的问题是GetDataCallback时间太长了。所以我的计数器i在设置文件名之前已经到了 5。所以我的解决方案只是在我的GetDataCallback. 如果您需要查看代码,请发表评论。

于 2013-08-14T00:18:51.373 回答