Donc, voici la chose, le fichier est téléchargé avec succès en tant que "photo.png" mais quand j'ouvre la photo du navigateur, la photo est corrompue. Lorsque je télécharge la photo pour l'ouvrir localement sur mon ordinateur, la photo est corrompue.Retrofit 2 succès de téléchargement d'image mais l'image est téléchargée comme fichier corrompu
Aucune idée pourquoi.
Heres le point final de rénovation:
@Multipart
@POST("alerts/{alertId}/photo/")
Call<Object> uploadPhotoStill(@Header("Authorization") String credentials, @Path("alertId") int alertId,
@Part("photo\"; filename=\"picture_taken.jpeg\" ") RequestBody photo);
Voici comment je l'utilise: je suis intercepta le tableau d'octets qui revient de prendre une photo avec l'objet de la caméra.
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);
}
});
}
Le succès est toujours appelé après le téléchargement de la photo. Mais la photo télécharge toujours corrompu et je n'ai aucune idée pourquoi.
est l'image au format PNG? – TheSunny
@TheSunny l'image est juste un byteArray saisi à partir de la méthode onPictureTaken – User051691
Alors, quel format est l'image? Le commentaire ci-dessus la méthode onPictureTaken indique que le format des données dépend des paramètres transmis. Vous devriez vérifier cela et voir si cela peut être la cause du problème. – TheSunny