当我从android应用程序上传loopj文件时,我遇到了一个非常奇怪的onprogress行为。
我正在使用synchhttpclient一个接一个地上载多个文件,
但是onporgress总是被一些看起来像硬编码或固定数字的数字触发,而不是二进制资产的实际大小和已经上传的字节。
下面是我得到的输出示例
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_客户端如下:
protected final static SyncHttpClient SYNC_HTTP_CLIENT = new SyncHttpClient();
static {
PersistentCookieStore cookieStore = new PersistentCookieStore(ApplicationContextProvider.getContext());
SYNC_HTTP_CLIENT.setCookieStore(cookieStore);
}
最佳答案
我确实想出来了,
它只适用于文件参数,不适用于流或bytearray——看起来像loopj中的一个bug。