将多个jpgs保存到SD卡

时间:2013-08-07 19:02:58

标签: android save jpeg counter parse-platform

我正在从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个图像?

2 个答案:

答案 0 :(得分:1)

在下载文件后执行GetDataCallback时,循环已运行5次,因此每次i的值为5。

答案 1 :(得分:0)

我的问题是GetDataCallback花了太长时间。所以我的计数器i在设置文件名之前达到了5。所以我的解决方案只是在GetDataCallback中添加另一个计数器。如果您需要查看代码,请注释。