Tengo una simple función de prueba para crear y compartir un archivo .csv que tras se ejecuta tras pulsar un botón. Al hacerlo me refleja el siguiente error.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.io.FileNotFoundException: qpon.csv: open failed: EROFS (Read-only file system)
at libcore.io.IoBridge.open(IoBridge.java:492)
at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
at java.io.FileOutputStream.<init>(FileOutputStream.java:125)
at java.io.FileWriter.<init>(FileWriter.java:63)
at com.ivandlrapp.virtualenconder.ui.history.HistoryBottomSheetDialog.exportCSV(HistoryBottomSheetDialog.kt:64)
at com.ivandlrapp.virtualenconder.ui.history.HistoryBottomSheetDialog.onViewCreated$lambda-3(HistoryBottomSheetDialog.kt:55)
at com.ivandlrapp.virtualenconder.ui.history.HistoryBottomSheetDialog.$r8$lambda$XOD8CuZgBTVKrUm_EEK1Lis60e0(Unknown Source:0)
at com.ivandlrapp.virtualenconder.ui.history.HistoryBottomSheetDialog$$ExternalSyntheticLambda3.onClick(Unknown Source:4)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
**Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)**
at libcore.io.Linux.open(Native Method)
at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7542)
at libcore.io.IoBridge.open(IoBridge.java:478)
at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
at java.io.FileOutputStream.<init>(FileOutputStream.java:125)
at java.io.FileWriter.<init>(FileWriter.java:63)
at com.ivandlrapp.virtualenconder.ui.history.HistoryBottomSheetDialog.exportCSV(HistoryBottomSheetDialog.kt:64)
at com.ivandlrapp.virtualenconder.ui.history.HistoryBottomSheetDialog.onViewCreated$lambda-3(HistoryBottomSheetDialog.kt:55)
at com.ivandlrapp.virtualenconder.ui.history.HistoryBottomSheetDialog.$r8$lambda$XOD8CuZgBTVKrUm_EEK1Lis60e0(Unknown Source:0)
at com.ivandlrapp.virtualenconder.ui.history.HistoryBottomSheetDialog$$ExternalSyntheticLambda3.onClick(Unknown Source:4)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Esta es la función:
private fun exportCSV(context: Context){
val csvHeader = "first, second"
val testFile = File.createTempFile("test", ".csv", context.cacheDir)
var fileWriter = FileWriter("test.csv")
fileWriter.append(csvHeader)
fileWriter.append('\n')
fileWriter.append("aaaaa")
fileWriter.append(',')
fileWriter.append("bbbbb")
fileWriter.append('\n')
fileWriter.close()
val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(context, "com.ivandlrapp.virtualenconder.fileprovider", testFile))
sendIntent.type = "text/csv"
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(Intent.createChooser(sendIntent, "SHARE"))
}
En el manifest tengo los permisos (también concedidos por el usuario) y el provider:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.ivandlrapp.virtualenconder.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Este es el documento files_paths.xml
<paths>
<external-cache-path name="images" path="/"/>
<cache-path name="docs" path="/"/>
</paths>
Obtengo el error en todas las versiones de Android. Gracias!