0

I bring you a curious case which has baffled me recently at my latest project.

Basically, I am trying to take a picture with camera app, save it to an Uri gotten from FileProvider and onActivityResults, get that image using the above mentioned Uri.

The following is the code I have used to dispatch the camera

public void dispatchCamera(String docName){
    String id=((FormsActivity)getActivity()).getIncidentId();

    filename = Environment.getExternalStorageDirectory().getPath() + "/DocPics/"+id+"_"
            +docsTakenCounter+".jpg";

    //outputFileUri = Uri.fromFile(new File(filename));
    outputFileUri = FileProvider.getUriForFile(getActivity().getApplicationContext(),
            BuildConfig.APPLICATION_ID +".fileprovider", new File(filename));

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    Log.e("DtAG", outputFileUri.toString());

    //Log.e("DT0", outputFileUri.toString());
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);


    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

Now, at this time, I have to say, previously, I had no need to use FileProvider to get an Uri to put into the MediaStore.EXTRA_OUTPUT, I used the simpler Uri.getUriFromFile. However, recent updates to Android made appear at compiling the issue of exposing Uri to an external app effectively forcing me to use FileProvider.

Now, this still works on my usual phone, the one I mainly develop with. However, since this particular app will require for it to be installed in a number of other different phones, and, these other phones fail to save the pictures taken with the FileProvider Uri, while keeping me from using the old way.

Below is also the sample codes for the requirements of FileProvider:

<provider
        android:name=".Formas.Actividades.Classes.GenericFileProvider"
        android:authorities="${applicationId}.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>

Next is this so called provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

And last but maybe the most important part, how I try to access the picture after returning from the camera.

I have tried this one:

try{
        InputStream ims = getActivity().getContentResolver().openInputStream(outputFileUri);
        // just display image in imageview
        bitmap = BitmapFactory.decodeStream(ims);
    }
    catch (Exception e)
    {
        //fail message
        return;
    }

As well as this variation:

try{
        bitmap = getSampleBitmapFromFile(filename, 100, 100);
    }catch (Exception e)
     {
     //the usual failed message toast
     }

After some tests I come to the conclusion at least the personal one, that I am either doing something really stupid, or the camera app is either not writing the picture in the passed Uri, or even worse, in my own inexperience I have been doing things, the novice way.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Check this: https://stackoverflow.com/a/18332000/5783417 – Atul Vasudev A Oct 13 '17 at 04:54
  • Atul Vasudev, thanks, i tried the solution in the link, but still the same, actually i had already tried granting the Uri permission with setFlags before. – Álvaro García Bojórquez Oct 13 '17 at 15:27
  • Hi there. A number of folks in our community sometimes say that every time they see gendered assumptions about software engineers, they worry about people feeling excluded. I wonder, could you try to avoid adding male-oriented greetings and pronouns in your posts, so as to make for a more welcoming environment? Thank you. – halfer Oct 17 '17 at 22:25
  • i understand, i will take it into consideration for future cases, so as to not offend anybody in particular, i will start looking for alternative phrasing so as to encompass a broader audience without discrimination. Having said that, i also encourage the members of the community, to consider how intentional towards those feeling excluded the phrasing of the post objectively is, that also taking into consideration the objective of the post itself, given that without said considering, well, it is also up to anyone to be offended by every letter in the alphabet. – Álvaro García Bojórquez Oct 19 '17 at 21:14

0 Answers0