How do I use Fileprovider and Bitmap together?

Asked

Viewed 134 times

0

I’m having trouble sending a photo to the server, specifically in the excerpt: "bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);". Always when taking the photo it shows error in this line. Previously instead of the "FileProvider.getUriForFile" used "Uri.fromFile" and functioned normally, but this method does not work in the Android 7.0 from then on, I had to switch to Fileprovider, but I can’t get the code to 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;
    }
  • Have you declared Fileprovider on Androidmanifest? If not, look at this reply.

  • Yes, I declared as follows: '<Provider android:name="android.support.v4.content.Fileprovider" android:Authorities="${applicationId}. fileprovider" android:Exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:Resource="@xml/filepaths"/> </Provider>'

  • I also created filepaths in xml as follows: '<paths> <External-path path="." name="mediaimages"/> </paths>'

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.