我想将位图另存为图像文件。我尝试了以下代码,但出现错误。

val dir = File("/tempfolder/")

if (!dir.exists()) {
    print("dir not exist");
    dir.mkdirs()
}

val output = File("tempfile.jpg")
var os: OutputStream? = null

os = FileOutputStream(output)

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os)

os.flush()
os.close()
错误:
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: java.io.FileNotFoundException: tempfile.jpg (Read-only file system)
        at java.io.FileOutputStream.open0(Native Method)
        at java.io.FileOutputStream.open(FileOutputStream.java:287)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:223)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
        at com.app.symbolrecogniser.CanV.touchUp(CanV.kt:190)
        at com.app.symbolrecogniser.CanV.onTouchEvent(CanV.kt:102)
        at android.view.View.dispatchTouchEvent(View.java:11776)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2968)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2657)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:448)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1829)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3307)
        at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:410)
        at android.view.View.dispatchPointerEvent(View.java:12015)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4795)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4609)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4293)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4174)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4350)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4200)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4166)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4174)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4147)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6661)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6635)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6596)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6764)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:186)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:325)
        at android.os.Looper.loop(Looper.java:142)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)```

最佳答案

您可能想要 Context#getDir

但是您可以访问其他一些内容,例如getCacheDir(用于临时性内容,当用户清除应用缓存或释放存储空间时将其擦除),getFilesDir(仅getDir在其中创建新文件夹的主文件夹,如果您不这样做)实际上需要一个单独的文件夹-在应用程序卸载时被擦除)和getExternalFilesDir(与getFilesDir相同,仅是在外部存储器上,如果您想放东西)。这些都是您的应用只能访问的私有(private)存储空间

07-27 16:48