Android Kotlin:无法将 InputStream 保存为文件

Posted

技术标签:

【中文标题】Android Kotlin:无法将 InputStream 保存为文件【英文标题】:Android Kotlin: unable to save InputStream as file 【发布时间】:2021-06-12 01:08:44 【问题描述】:

我正在使用 Kotlin 构建一个 android 应用程序。在我的应用程序中,我试图将 InputStream 保存为文件。但它不起作用。

这是将 InputStream 保存为文件的函数

fun copyStreamToFile(inputStream: InputStream) 
        var outputFile = File("testing-again.png")
        inputStream.use  input ->
            val outputStream = FileOutputStream(outputFile)
            outputStream.use  output ->
                val buffer = ByteArray(4 * 1024) // buffer size
                while (true) 
                    val byteCount = input.read(buffer)
                    if (byteCount < 0) break
                    output.write(buffer, 0, byteCount)
                
                output.flush()
            
        
    

它给了我以下错误

Process: com.forkthecoop.com22222, PID: 16949
    java.lang.RuntimeException: Failure delivering result ResultInfowho=null, request=546, result=-1, data=Intent  dat=content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/31/ORIGINAL/NONE/image/jpeg/323542094 flg=0x1 clip=text/uri-list ...  to activity com.forkthecoop.com22222/com.forkthecoop.com22222.SendOrdinaryMessageActivity: java.io.FileNotFoundException: testing-again.png: open failed: EROFS (Read-only file system)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5015)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        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: java.io.FileNotFoundException: testing-again.png: 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:186)
        at com.forkthecoop.com22222.PartialMediaUploadPreview.copyStreamToFile(PartialMediaUploadPreview.kt:171)
        at com.forkthecoop.com22222.PartialMediaUploadPreview.handleOnActivityResult(PartialMediaUploadPreview.kt:146)
        at com.forkthecoop.com22222.SendOrdinaryMessageActivity.onActivityResult(SendOrdinaryMessageActivity.kt:208)
        at android.app.Activity.dispatchActivityResult(Activity.java:8310)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5008)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056) 
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        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:186) 
        at com.forkthecoop.com22222.PartialMediaUploadPreview.copyStreamToFile(PartialMediaUploadPreview.kt:171) 
        at com.forkthecoop.com22222.PartialMediaUploadPreview.handleOnActivityResult(PartialMediaUploadPreview.kt:146) 
        at com.forkthecoop.com22222.SendOrdinaryMessageActivity.onActivityResult(SendOrdinaryMessageActivity.kt:208) 
        at android.app.Activity.dispatchActivityResult(Activity.java:8310) 
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5008) 
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056) 
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        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) 
2021-03-14 15:56:54.505 518-2903/system_process E/WifiThreadRunner: WifiThreadRunner.call() timed out!
    java.lang.Throwable: Stack trace:
        at com.android.server.wifi.WifiThreadRunner.call(WifiThreadRunner.java:80)
        at com.android.server.wifi.WifiServiceImpl.startScan(WifiServiceImpl.java:497)
        at android.net.wifi.IWifiManager$Stub.onTransact(IWifiManager.java:718)
        at android.os.Binder.execTransactInternal(Binder.java:1159)
        at android.os.Binder.execTransact(Binder.java:1123)

我的应用程序对存储具有读取和写入权限。它只是不工作。我的代码有什么问题,我该如何解决?

【问题讨论】:

我不确定错误是什么。从那个错误来看,Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)Failure delivering result ResultInfowho=null, request=546, result=-1, data=Intent dat=content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/31/ORIGINAL/NONE/image/jpeg/323542094 flg=0x1 clip=text/uri-list ... to activity com.forkthecoop.com22222/com.forkthecoop.com22222.SendOrdinaryMessageActivity: java.io.FileNotFoundException: testing-again.png: open failed: EROFS (Read-only file system) 所以,我认为您遇到了错误,因为您无权在该路径/目录上执行某些操作。 var outputFile = File("testing-again.png") 供应的完整路径。不仅仅是一个文件名。 这是因为您尝试将图像保存在此路径中:/testing-again.png 并且它是系统文件 【参考方案1】:

FileNotFoundException: testing-again.png: 打开失败: EROFS (只读文件系统)

您无法将文件保存在系统文件夹中,因此您必须指定路径并保存图像,例如我将图像保存在下面的 DCIM 文件夹中:

fun copyStreamToFile(inputStream: InputStream) 
    val storeDirectory = this.getExternalFilesDir(Environment.DIRECTORY_DCIM) // DCIM folder
    val outputFile = File(storeDirectory, "testing-again.png")
    inputStream.use  input ->
        val outputStream = FileOutputStream(outputFile)
        outputStream.use  output ->
            val buffer = ByteArray(4 * 1024) // buffer size
            while (true) 
                val byteCount = input.read(buffer)
                if (byteCount < 0) break
                output.write(buffer, 0, byteCount)
            
            output.flush()
        
    

你会发现你的形象 在此路径中:/storage/emulated/0/Android/data/Your_Package_Name/files/DCIM

【讨论】:

当我按照您的代码更改路径时出现以下错误。 2021-03-14 17:19:14.300 18978-18978/com.forkthecoop.com22222 E/AndroidRuntime:致命异常:主进程:com.forkthecoop.com22222,PID:18978 java.lang。 RuntimeException: 传递结果失败 ResultInfowho=null, request=546, result=-1, data=Intent dat=content://com.google.android.apps.photos.contentprovider/-1/1/content:/ /media/external/images/media/31/ORIGINAL/NONE/image/jpeg/1216397895 flg=0x1 clip= 你在问题​​中提到的错误是一样的吗!? java.io.FileNotFoundException: testing-again.png: open failed: EROFS (Read-only file system) ? 我刚刚在聊天室发了一条消息。请看一看。

以上是关于Android Kotlin:无法将 InputStream 保存为文件的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 Kotlin Retrofit 将数据从 Android 插入到 Mysql [重复]

自定义ConstraintLayout类似乎无法横向排列 - Android - Kotlin

在 Kotlin/Android Studio 中无法将 mp3 转换为 pcm - UnsupportedAudioFileException

Android kotlin:无法读取类文件

我似乎无法将我的Kotlin Recycler多视图代码整理出来

将现有 Flutter Java Android 项目转换为 Kotlin