I'm uploading an image with retrofit. The problem is that Huawei does not work well and falls. Apparently it does not locate the path of the file.
This is the upload code.
final UtlLoading cargarFoto = new UtlLoading(this, "Cargando foto", false);
cargarFoto.show();
try {
db.documentosUpdateNumSolicitudOnline(docu.getId(), codigo_solicitud);
Map<String, RequestBody> map = new HashMap<>();
File file = new File(docu.getEnlace());
file = cambiarTamanio(file);
final String mName = file.getName();
String mTxtPublicacion = "";
mTxtPublicacion = mName;
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
RequestBody descId = RequestBody.create(MediaType.parse("text/plain"), mTxtPublicacion);
map.put("txtarchivo\";filename=\"" + mTxtPublicacion + "\"", requestBody);
ServerConfig getResponse = ApiServer.getRetrofit().create(ServerConfig.class);
Call<ServerResponse> responseCall = getResponse.uploadFileServer("token", map, descId);
responseCall.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
ServerResponse serverResponse = response.body();
if (serverResponse != null) {
if (serverResponse.getSuccess() == 1) {
Toast.makeText(getApplicationContext(), "Imagen subida exitosamente", Toast.LENGTH_LONG).show();
String ruta = serverResponse.getMessage();
try {
actualizarDataFoto(docu.getId(), codigo_solicitud, settings.getString("username",""), docu.getCodigo_grupo(),mName,(ruta+mName),funciones.ObtenerIpAddress(),funciones.ObtenerMac());
} catch (JSONException e) {
e.printStackTrace();
}
}else {
Toast.makeText(getApplicationContext(), serverResponse.getMessage(), Toast.LENGTH_LONG).show();
}
}else {
Log.v("Response: ", serverResponse.toString());
}
cargarFoto.hide();
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
Log.d("Error: ","Error al subir archivo:" + t.getMessage());
t.printStackTrace();
String msg = t.getMessage();
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
cargarFoto.hide();
}
});
}catch (Exception ex){
System.out.println("Error en: " + ex.getMessage());
cargarFoto.hide();
}
This is the photo-taking code.
imTomarFoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Toast.makeText(NuevaSolicitud10Activity.this, "Error", Toast.LENGTH_SHORT).show();
}
finally {
if (photoFile != null) {
mCurrentPhotoPath = Uri.parse(photoFile.getAbsolutePath());
Uri photoURI = FileProvider.getUriForFile(NuevaSolicitud10Activity.this, "fuvex.credinka.fuvex.fileprovider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, TAKE_PROPIEDAD);
}
}
}
}
else{
Toast.makeText(NuevaSolicitud10Activity.this, "Ya no puede agregar mas fotos en esta sección.", Toast.LENGTH_SHORT).show();
}
});
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
return image;
}