【问题标题】:Sharing logfile with FileProvider fails与 FileProvider 共享日志文件失败
【发布时间】:2018-10-18 08:23:22
【问题描述】:

我正在尝试使用 FileProvider 内容提供程序共享我的内部日志文件。我在清单中有以下<provider> 条目:

<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>

file_provider_paths.xml 包含:

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

创建分享意图的代码是:

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;
}

意图的创建正在正常工作,但是当我尝试与例如分享时Gmail 我在 logcat 中收到以下错误:

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

我已经搜索了答案,但找不到。那么关于我做错了什么有什么建议吗?

【问题讨论】:

  • 您可以尝试以下更改您的“file_provider_paths.xml”并让我知道更新:&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;paths&gt; &lt;external-files-path name="log" path="." /&gt; &lt;/paths&gt;
  • 我刚刚提供了一个解决问题的答案。

标签: android android-fileprovider


【解决方案1】:

经过一番搜索后,我发现我使用了错误的位置来保存我的日志文件。为了解决这个问题,我必须对 logback.xml 配置进行以下更改:

<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>

相关更改是属性EXT_FILES_DIR 的值。这现在将使用应用程序可用的外部存储,而不是旧路径 /data/data/nl.charm.nedradio/files

需要的其他更改在file_provider_paths.xml 中。这里外部路径由"."改为"/"

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

现在共享适用于例如Gmail 和 Google 云端硬盘。

【讨论】:

    【解决方案2】:

    这是一个类似的问题,我的回答中有一个picture。请检查this question,我保证可以解决您的问题。

    <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).
    

    【讨论】:

    • 这确实是我发现的答案之一,对找到正确的解决方案非常有帮助。
    • 如果我的回答对你有帮助,请选择我的回答,不胜感激,提前谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 2014-05-13
    • 1970-01-01
    • 2012-03-09
    相关资源
    最近更新 更多