我正在從parse.com下載圖像。我永遠不知道我將擁有的圖像數量,所以我正在使用計數器來檢索它們。出於某種原因,我的計數器達到5,並下載圖像5次。在我的存儲中,我只有一個圖像。這裏是我的代碼:將多個jpgs保存到SD卡
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個圖像?
我可以從我的logcat中看到。但是爲什麼5次不運行我的'GetDataCallback',所以我可以下載所有5個圖像? –
我明白我的'getdatacallback'只能在第5個循環中運行。我的問題赫克託,是我如何讓我的數據通話重新運行5次? –
它正在運行五次,正如您在日誌中看到的那樣。問題在於在執行回調之前使用一個正在變異的變量。 –