改进2图像上传成功但图像上传为损坏的文件

时间:2016-02-28 19:28:01

标签: java android retrofit retrofit2

所以事实上,该文件已成功上传为" photo.png"但是当我从浏览器打开照片时,照片已损坏。当我下载照片以在我的计算机上本地打开时,照片已损坏。

不知道为什么。

继承改造终点:

@Multipart
@POST("alerts/{alertId}/photo/")
Call<Object> uploadPhotoStill(@Header("Authorization") String credentials, @Path("alertId") int alertId,
                              @Part("photo\"; filename=\"picture_taken.jpeg\" ") RequestBody photo);

以下是我如何使用它:我正在抓取使用相机对象拍摄的字节数组。

private void initCamera() {
    if (camera == null) {
        camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);

        Camera.Parameters params = camera.getParameters();
        params.setPictureFormat(ImageFormat.JPEG);

        camera.setParameters(params);
    }
}

/**
 * Called when image data is available after a picture is taken.
 * The format of the data depends on the context of the callback
 * and {@link Camera.Parameters} settings.
 *
 * @param data   a byte array of the picture data
 * @param camera the Camera service object
 */
@Override
public void onPictureTaken(byte[] data, Camera camera) {
    Log.d(TAG, "onPictureTaken() called with: " + "data = [" + Arrays.toString(data) + "], camera = [" + camera + "]");
    handleTakenStill(data);
}

private void handleTakenStill(byte[] data) {
    Log.d(TAG, "handleTakenStill() was called");

    RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpeg"), data);

    havenApi.uploadPhotoStill(Utils.encodeUserCredentials(), getCurrentAlert().getId(), requestBody).enqueue(new Callback<Object>() {
        @Override
        public void onResponse(Response<Object> response, Retrofit retrofit) {
            if (response.isSuccess()) {
                Log.d(TAG, "handleTakenStill.onResponse success: " + response.body().toString());
            } else {
                Log.e(TAG, "handleTakenStill.onResponse error: " + response.message());
            }
        }

        @Override
        public void onFailure(Throwable t) {
            Utils.logOnFailureRequest(TAG, t);
        }
    });
}

上传照片后始终会调用成功。但照片总是上传已损坏,我不知道为什么。

3 个答案:

答案 0 :(得分:0)

1)按照评论中的说明检查照片类型。有时候用JPG格式解决问题。

2)尝试将参数作为参数放入RequestBody文件或文件路径(RequestBode对象的另一个构造函数)

我使用下面的代码没有问题,但我发送文件作为表格数据:

@Multipart
    @POST("/api/tour/upload")
    Call<MainFileModel> uploadTourFiles(@PartMap Map<String, RequestBody> params);


    File mainFile = new File(fileLoad.getFile());
    Map params = new Map<>();
    params.put("file\"; filename=\"" + mainFile.getName(),
      RequestBody.create(MediaType.parse("multipart/form-data"),                                 mainFile));; 


//And Make call here!

答案 1 :(得分:0)

我从来没有通过改造来实现这一目标。但我能用OkHttp做到这一点。

如果您想知道解决方案的解决方法:

/**
 * Called when image data is available after a picture is taken.
 * The format of the data depends on the context of the callback
 * and {@link Camera.Parameters} settings.
 *
 * @param data   a byte array of the picture data
 * @param camera the Camera service object
 */
@Override
public void onPictureTaken(byte[] data, Camera camera) {
    Log.d(TAG, "onPictureTaken() called with: " + "data = [" + Arrays.toString(data) + "], camera = [" + camera + "]");
    handleTakenStill(data);
}

private void handleTakenStill(byte[] data) {
    Log.d(TAG, "handleTakenStill() was called");

    Observable.create(uploadPhoto(data))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<com.squareup.okhttp.Response>() {
                @Override
                public void onCompleted() {
                    Log.d(TAG, "onCompleted() was called");
                }

                @Override
                public void onError(Throwable e) {
                    Utils.logOnFailureRequest(TAG, e);
                }

                @Override
                public void onNext(com.squareup.okhttp.Response response) {
                    if (response.isSuccessful()) {
                        Log.d(TAG, "handleTakenStill.onResponse success: " + response.body().toString());
                    } else {
                        Log.e(TAG, "handleTakenStill.onResponse error: " + response.message());
                    }
                }
            });
}

@NonNull
private Observable.OnSubscribe<com.squareup.okhttp.Response> uploadPhoto(final byte[] data) {
    return new Observable.OnSubscribe<com.squareup.okhttp.Response>() {
        @Override
        public void call(Subscriber<? super com.squareup.okhttp.Response> subscriber) {
            OkHttpClient client = new OkHttpClient();

            RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
                    .addFormDataPart("camera", "picture_taken.jpg", RequestBody.create(MediaType.parse("image/jpeg"), data))
                    .build();

            Request request = new Request.Builder()
                    .url(url)
                    .post(requestBody)
                    .addHeader("content-type", "multipart/form-data")
                    .addHeader("authorization", Utils.encodeUserCredentials())
                    .addHeader("accept", "application/json")
                    .addHeader("cache-control", "no-cache")
                    .build();

            try {
                com.squareup.okhttp.Response response = client.newCall(request).execute();
                subscriber.onNext(response);
                subscriber.onCompleted();
                if (!response.isSuccessful()) {
                    subscriber.onError(new Exception("Error uploading photo"));
                }
            } catch (IOException e) {
                subscriber.onError(e);
            }
        }
    };
}

答案 2 :(得分:0)

我也有这个问题。我没有使用 MultiPart 表单,而是对适合我的图像数据使用了一个简单的 RequestBody。我在一个 Android 项目中使用 Kotlin。

我的改造 API:

interface FilesApi {
    @PUT("/files/attachments/{parentId}")
    fun uploadAttachment(
        @Path("parentId") parentId: String,
        @Header("Content-Type") contentType: String,
        @Header("Name") name: String,
        @Header("Description") description: String?,
        @Body image: RequestBody
    ): Single<Response<FileUploadResponse>>
}

创建请求对象:

imageData 变量是一个 ByteArray

val requestFile = imageData.toRequestBody(contentType.toMediaTypeOrNull(), 0, imageData.size)

return filesApi.uploadAttachment(parentId, contentType, name, description, requestFile)
            .map { response ->
                response.code()
                response.errorBody()
                response.body()?.Result
            }