共享由 MODE_PRIVATE 中的应用程序创建的文件

Sharing file create by an app in MODE_PRIVATE

所以我有一个应用程序通过 MODE_PRIVATE[=15= 中的 FileOutputStream 将传感器数据写入 csv 文件]

FileOutputStream fos = null;
        try {
            fos = context.openFileOutput(FILE_NAME,Context.MODE_PRIVATE);
            for(int i = 0; i< signalList.size(); i++){
                Signal s = signalList.get(i);
                fos.write(s.toString().getBytes());
            }
            Log.d("WRITESTATUS","Success");
        }

现在我想与其他应用程序(whatsapp、电子邮件等)共享此文件。为此,我使用下面的代码来分享意图。我试图使用 MODE_WORLD..something 但这没有用,因为它已被弃用,所以我通过其他 Whosebug 问题了解到我们需要使用 FileProvider 来实现这一点,但这也没有用。


        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("*/*");
        File file = getFileStreamPath(FILE_NAME);
        Uri uri = FileProvider.getUriForFile(this,"com.example.writefile",file);
        sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
        sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(Intent.createChooser(sharingIntent, "Share SENSOR Data"));


         <provider
            android:authorities="com.example.writefile"
            android:name="androidx.core.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_path"/>
        </provider>

错误日志



    2020-06-22 16:27:34.047 6578-6578/? E/ample.writefil: Unknown bits set in runtime_flags: 0x8000
    2020-06-22 16:27:34.147 6578-6578/com.example.writefile E/libc: Access denied finding property "vendor.iop.enable_uxe"
    2020-06-22 16:27:36.043 6578-6578/com.example.writefile E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.writefile, PID: 6578
        java.lang.IllegalStateException: Could not execute method for android:onClick
            at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
            at android.view.View.performClick(View.java:7158)
            at android.view.View.performClickInternal(View.java:7135)
            at android.view.View.access00(View.java:801)
            at android.view.View$PerformClick.run(View.java:27373)
            at android.os.Handler.handleCallback(Handler.java:883)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:235)
            at android.app.ActivityThread.main(ActivityThread.java:7441)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
         Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
            at android.view.View.performClick(View.java:7158) 
            at android.view.View.performClickInternal(View.java:7135) 
            at android.view.View.access00(View.java:801) 
            at android.view.View$PerformClick.run(View.java:27373) 
            at android.os.Handler.handleCallback(Handler.java:883) 
            at android.os.Handler.dispatchMessage(Handler.java:100) 
            at android.os.Looper.loop(Looper.java:235) 
            at android.app.ActivityThread.main(ActivityThread.java:7441) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935) 
         Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.writefile/files/sensor.csv
            at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
            at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
            at com.example.writefile.MainActivity.Clicked(MainActivity.java:266)
            at java.lang.reflect.Method.invoke(Native Method) 
            at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) 
            at android.view.View.performClick(View.java:7158) 
            at android.view.View.performClickInternal(View.java:7135) 
            at android.view.View.access00(View.java:801) 
            at android.view.View$PerformClick.run(View.java:27373) 
            at android.os.Handler.handleCallback(Handler.java:883) 
            at android.os.Handler.dispatchMessage(Handler.java:100) 
            at android.os.Looper.loop(Looper.java:235) 
            at android.app.ActivityThread.main(ActivityThread.java:7441) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935) 


改变这个,它会起作用 - 我从 here 那里得到提示并尝试了不同的配置并且这有效,我不知道为什么。

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
        <files-path
            name="sensor"
            path="."/>
</paths>