1

I'm trying to share my internal logfile with the use of the FileProvider content provider. I have the following <provider> entry in the manifest:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="nl.charm.nedradio"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_provider_paths" />
</provider>

The file_provider_paths.xml contains:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path name="files" path="." />
</paths>

The code to create the share intent is:

private static final String LOGFILE_NAME = "log.txt";
private static final String AUTHORITY = "nl.charm.nedradio";

public static Intent getShareIntent(Context context)
{
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "Log File");
    intent.putExtra(Intent.EXTRA_TEXT, "App logfile.");

    // Allow access outside of share application's realm
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    File logFile = new File(context.getExternalFilesDir(null), LOGFILE_NAME);
    Uri logURI = FileProvider.getUriForFile(context, AUTHORITY, logFile);
    intent.putExtra(Intent.EXTRA_STREAM, logURI);

    return intent;
}

The creation of the intent is working as it should but when I try to share with e.g. Gmail I get the following error in logcat:

2018-10-18 10:16:49.536 4585-4585/com.google.android.gm E/Gmail: Gmail:Error adding attachment
    exk: FileNotFoundException when openFileDescriptor.

I've searched for an answer but couldn't find one. So any suggestions on what I'm doing wrong?

Kees de Bruin
  • 175
  • 2
  • 16

2 Answers2

0

After some more searching I found that I'm using the incorrect location to save my log files. To solve the issue I had to make the following changes to the logback.xml configuration:

<configuration>

    <property name="LOGFILE_NAME" value="log.txt" />
    <property name="EXT_FILES_DIR" value="${EXT_DIR:-/sdcard}/Android/data/${PACKAGE_NAME}/files" />

    <appender name="rollingfile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${EXT_FILES_DIR}/${LOGFILE_NAME}</file>

        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOGFILE_NAME}.%i</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>1</maxIndex>
        </rollingPolicy>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>400KB</maxFileSize>
        </triggeringPolicy>

        <encoder>
            <pattern>%date %-5level %logger{10} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="TRACE">
        <appender-ref ref="rollingfile" />
    </root>

</configuration>

The relevant change is the value of property EXT_FILES_DIR. This will now use the external storage available to the application instead of the old path /data/data/nl.charm.nedradio/files.

The other change needed is in the file_provider_paths.xml. Here the external path is changed from "." to "/":

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path name="files" path="/" />
</paths>

Now sharing works with e.g. Gmail and Google Drive.

Kees de Bruin
  • 175
  • 2
  • 16
-1

Here is a similar question,thers is a picture which is in my answer.please check this question and I promise that can fix your problem.

<files-path name="name" path="path" />  
Represents files in the files/ subdirectory of your app's internal storage area. This subdirectory is the same as the value returned by Context.getFilesDir().

<external-path name="name" path="path" />
Represents files in the root of the external storage area. The root path of this subdirectory is the same as the value returned by Environment.getExternalStorageDirectory().

<external-files-path name="name" path="path" />
Represents files in the root of your app's external storage area. The root path of this subdirectory is the same as the value returned by Context#getExternalFilesDir(String) Context.getExternalFilesDir(null).
aolphn
  • 2,950
  • 2
  • 21
  • 30