3

从android应用程序上传带有loopj的文件时,我遇到了onProgress的一个非常奇怪的行为。我正在使用 SyncHttpClient 一个接一个地上传多个文件,但是 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_CLIENT 在基类中定义如下:

protected final static SyncHttpClient SYNC_HTTP_CLIENT = new SyncHttpClient();

static {
    PersistentCookieStore cookieStore = new PersistentCookieStore(ApplicationContextProvider.getContext());
    SYNC_HTTP_CLIENT.setCookieStore(cookieStore);

}
4

2 回答 2

1

我确实弄清楚了,它仅适用于 File 参数,不适用于 Stream 或 ByteArray - 似乎是 loopj 中的一个错误。

于 2014-07-30T23:17:22.850 回答
0

如果您查看 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 中可能还会考虑任何请求标头。

于 2014-12-16T15:37:17.113 回答