我遇到了一个非常奇怪的onProgress的行为,当上传文件与loopj从android应用程序。我上传多个文件一个接一个使用SyncHttpClient,但是onprogress总是被一些看起来像硬编码或固定数字的数字所触发,而不是二进制资产和已经上传的字节的实际大小。下面是我得到的示例输出
V/AsyncHttpResponseHandler﹕ Progress 3353492 from 3353528 (100%)
V/AsyncHttpResponseHandler﹕ Progress 3353528 from 3353528 (100%)
V/AsyncHttpResponseHandler﹕ Progress 1338 from 2417 (55%)
V/AsyncHttpResponseHandler﹕ Progress 2417 from 2417 (100%)
然后对每个上传的文件执行相同的调用顺序。任何想法都将非常感激。顺便说一句,文件上传得很好,我只是不能让加载指示器正常工作。
下面是代码示例: @Override
protected void onCreate(Bundle savedInstanceState) {
...
waveOperation = new WaveOperation();
waveOperation.execute();
}
//inner asynchTask class
private class WaveOperation extends AsyncTask<Void, Object, String> {
@Override
protected String doInBackground(Void... arg0) {
waveAll(ApplicationContextProvider.getContext());
return "Executed";
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {
}
private void waveAll(Context context) {
String[] projection = new String[]{
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.MIME_TYPE,
MediaStore.Images.ImageColumns.ORIENTATION
};
String selection = MediaStore.Images.Media.DATE_TAKEN + " > ?";
String[] selectionArgs = {String.valueOf(ApplicationContextProvider.getCurrentAssetDateTime().getTime())};
final Cursor cursor = context.getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection,
selectionArgs, MediaStore.Images.ImageColumns.DATE_TAKEN + " ASC");
while (cursor.moveToNext() && !isCancelled()) {
final String imageLocation = cursor.getString(1);
File imageFile = new File(imageLocation);
if (imageFile.exists()) { // is there a better way to do this?
Bitmap bm = BitmapFactory.decodeFile(imageLocation);
int orientation = cursor.getInt(5);
// Log.d("###################### orientation: ", String.valueOf(orientation));
long timeTaken = cursor.getLong(3);
final String dateTaken = simpleDateFormat.format(timeTaken);
...
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); // rotating bitmap
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
try {
EWImage.uploadPhoto(stream.toByteArray(), dateTaken + ".jpg", new AsyncHttpResponseHandler() {
//this is the method that is not being fired often enough during the upload
//and when it's called, it report some weird numbers that look always the same regardles
//of file being uploaded
@Override
public void onProgress(int bytesWritten, int totalSize) {
super.onProgress(bytesWritten, totalSize);
Log.d("--------------progress: ", String.valueOf(bytesWritten) + " of " + String.valueOf(totalSize));
}
@Override
public void onStart() {
super.onStart();
...
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
...
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
...
}
@Override
public void onFinish() {
super.onFinish();
}
}
);
} catch (FileNotFoundException e) {
Log.e("FileNotFound", e.toString());
e.printStackTrace();
}
}
}
}
}
下面是EWImage类中的uploadPhoto方法:
public static void uploadPhoto(byte[] photoByteArray, String photoName, AsyncHttpResponseHandler responseHandler) throws FileNotFoundException {
responseHandler.setUseSynchronousMode(true);
RequestParams params = new RequestParams();
params.put("file", new ByteArrayInputStream(photoByteArray), photoName);
UploadProgressActivity.currentRequestHandle =
SYNC_HTTP_CLIENT.post(getAbsoluteUrl("/upload"), params, responseHandler);
}
和在基类中定义的SYNC_HTTP_CLIENT:
protected final static SyncHttpClient SYNC_HTTP_CLIENT = new SyncHttpClient();
static {
PersistentCookieStore cookieStore = new PersistentCookieStore(ApplicationContextProvider.getContext());
SYNC_HTTP_CLIENT.setCookieStore(cookieStore);
}
我明白了,它只适用于File参数,不适用于Stream或ByteArray——似乎是loopj中的一个bug。
如果你检查AsyncHttpResponseHandler的源代码,你会注意到"sendProgressMessage"在获得请求响应时也被调用。这就是你看到的最后两个更新:
V/AsyncHttpResponseHandler﹕ Progress 1338 from 2417 (55%)
V/AsyncHttpResponseHandler﹕ Progress 2417 from 2417 (100%)
关于首次更新:
V/AsyncHttpResponseHandler﹕ Progress 3353492 from 3353528 (100%)
V/AsyncHttpResponseHandler﹕ Progress 3353528 from 3353528 (100%)
请记住,可能在totalSize中也会考虑任何请求头。