I'm having trouble sending a photo to the server, specifically in the snippet: "bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);"
. Always when taking the photo it presents an error in this line. Previously, instead of "FileProvider.getUriForFile"
used "Uri.fromFile"
and it worked normally, but this method does not work in Android 7.0
on, so I had to switch to FileProvider, but I can not make the code work. Does anyone know a solution to this problem ???
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
//Metodo para tirar foto.
@Override
public void onClick(View v) {
i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, file_uri);
startActivityForResult(i, 10);
getFileUri();
}
});
private void getFileUri() {
image_name = "testing123.jpg";
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ File.separator + image_name
);
String authorities = getApplicationContext().getPackageName() + ".fileprovider";
file_uri = FileProvider.getUriForFile(CarActivity.this, authorities, file);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 10 && resultCode == RESULT_OK) {
new Encode_image().execute();
}
}
private class Encode_image extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
bitmap = BitmapFactory.decodeFile(file_uri.getPath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
bitmap.recycle();
byte[] array = stream.toByteArray();
encoded_string = Base64.encodeToString(array, 0);
return null;
}