Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 与FileProvider共享日志文件失败_Android_Android Fileprovider - Fatal编程技术网

Android 与FileProvider共享日志文件失败

Android 与FileProvider共享日志文件失败,android,android-fileprovider,Android,Android Fileprovider,我正在尝试使用FileProvider内容提供程序共享我的内部日志文件。我在舱单中有以下条目: <provider android:name="android.support.v4.content.FileProvider" android:authorities="nl.charm.nedradio" android:exported="false" android:grantUriPermissions="true"> <meta-d

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

<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>
创建共享意图的代码是:

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.

我一直在寻找答案,但没有找到。有没有关于我做错了什么的建议?

经过进一步搜索,我发现我使用了不正确的位置来保存日志文件。为了解决这个问题,我必须对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>

现在共享可以与Gmail和Google Drive一起使用。

这里有一个类似的问题,我的答案中有一个。请检查,我保证可以解决您的问题

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

您能否尝试在您的文件\u provider\u paths.xml中进行以下更改,并让我知道更新内容:我刚刚提供了一个解决问题的答案。这确实是我发现的答案之一,对找到合适的解决方案非常有帮助。如果我的答案对您有帮助,请选择我的答案,我们将不胜感激,提前感谢。
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path name="files" path="/" />
</paths>
<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).