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.