1

I am opening the camera to capture a image and i followed this tutorial. Error stated in the title comes when i tried to create different flavor builds configuration for my app.

This is my meta data xml resource file named file_paths for File Provider path configuration -

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

I am setting it in manifest as follows -

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="@string/authority"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

and i am instantiating string resource named "images_file_path" from gradle build file according to build congiguration like this -

productFlavors {
    dev {
        resValue "string", "images_file_path","\"Android/data/com.swinguff.android.dev/files/Pictures\""
    }
    prod {
        resValue "string", "images_file_path","\"Android/data/com.swinguff.android/files/Pictures\""
    }

and i am launching camera in my activity like this -

File photoFile = null;
        try {
            photoFile = createImageFile();
            if (photoFile != null) {
            /*Following line is throwing the error */
            profilePicUri =
            FileProvider.getUriForFile(ApplicationContext.getAppContext(),
                        BuildConfig.AUTHORITY,
                        photoFile);
                launchcamera();
                //Log.d("Musik","camera bug take photo"+photoFile+" "+mPhotoPathForCamera+" "+profilePicUri);
            }
        } catch (Exception ex) {
            // Error occurred while creating the File
            Log.d("CAMERA_BUG","exception "+ex.getMessage()+"\n"+BuildConfig.APPLICATION_ID+"\n"+BuildConfig.AUTHORITY
                +"\n"+getString(R.string.images_file_path));
            Util.showSnackbar(xRoot,getString(R.string.camera__open_error));

        }

private File createImageFile() throws IOException {
    // Create an image file name
    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 */
    );

    // Save a file: path for use with ACTION_VIEW intents
    npath = image.getAbsolutePath();
    return image;
}

Same code does not cause any exception when i set value of path property in above meta data xml resource as a literal string and not by a resouce string value.

I don't understand why this is happening.

Please let me know if i can explain better.Please help and thanks in advance.

Devansh Kumar
  • 1,364
  • 1
  • 13
  • 26
  • You can simply put file_paths.xml file into your flavors paths. Like dev/main/src/res/xml/file_paths.xml and prod/main/src/res/xml/file_paths.xml. And just write full path without using string resource and resValue config. – Northern Poet Sep 22 '17 at 07:31
  • @Miller Thank you so much , your solution works .....but i still don't get why it is not setting properly by resource string – Devansh Kumar Sep 22 '17 at 07:41
  • Using string resource, you can just override this string resource in your strings.xml for different flavours. No need for productFlavors section in gradle config. I guess it's a root of problem, compiler can't use gradle config to decode string value. – Northern Poet Sep 22 '17 at 07:45
  • @Miller ahh..okay i got it....Thanks for the help – Devansh Kumar Sep 22 '17 at 07:48

2 Answers2

0

as Miller pointed out in the comments the solution is to put meta data xml resource file file_paths.xml separately for each build in their corresponding (dev/res/xml or prod/res/xml) folder and set corresponding path value directly in xml file and not by string resource images_file_path

Devansh Kumar
  • 1,364
  • 1
  • 13
  • 26
0
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

see this edit your paths like this your problem will solve