我无法获取目录,我正在使用的IDE是使用Kotlin语言的Android Studio,我已经输入了图像目录的确切路径,即“ D:\ Fyp \ app \ src \ main \ res \ drawable \ picture” .jpg”,我已经尝试使用相同的代码在IntelliJ上运行,但效果也很好,但是对于android studio来说,它却无法正常工作...下面是代码...
private fun encoder(): String {
val bytes = File("D:\\Fyp\\app\\src\\main\\res\\drawable\\picture.jpg").readBytes()
val base64 = Base64.getEncoder().encodeToString(bytes)
return base64
}
这些是错误日志...
2019-12-16 20:26:24.415 15322-15322/com.example.fyp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fyp, PID: 15322
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fyp/com.example.fyp.MainActivity}: java.io.FileNotFoundException: D:\Fyp\app\src\main\res\drawable\image.jpg: open failed: ENOENT (No such file or directory)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
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:930)
Caused by: java.io.FileNotFoundException: D:\Fyp\app\src\main\res\drawable\image.jpg: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:496)
at java.io.FileInputStream.<init>(FileInputStream.java:159)
at kotlin.io.FilesKt__FileReadWriteKt.readBytes(FileReadWrite.kt:63)
at com.example.fyp.MainActivity.encoder(MainActivity.kt:62)
at com.example.fyp.MainActivity.onCreate(MainActivity.kt:57)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
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:930)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Linux.open(Native Method)
at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7255)
at libcore.io.IoBridge.open(IoBridge.java:482)
at java.io.FileInputStream.<init>(FileInputStream.java:159)
at kotlin.io.FilesKt__FileReadWriteKt.readBytes(FileReadWrite.kt:63)
at com.example.fyp.MainActivity.encoder(MainActivity.kt:62)
at com.example.fyp.MainActivity.onCreate(MainActivity.kt:57)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
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:930)
答案 0 :(得分:1)
在Android应用程序中使用PC上文件的路径是没有意义的。
此外,资源(项目的res
目录中的任何资源)都不是已编译应用程序中的传统文件。您无法将它们作为文件访问,并且从商店安装应用程序后,它们甚至可能甚至不存在于应用程序中(如果根据设备特征将其滤除)。
要在应用程序中包含可以在运行时加载的文件,则需要将它们放在assets
目录中,而不要放在res
中。如果您还没有assets
目录,请在src/main
中创建一个目录。然后将文件放入其中。您可以这样阅读:
val bytes = context.assets.open("picture.jpg").use { it.readBytes() }
请注意,使用use
包装了readBytes()
调用。这是在完成阅读后关闭输入流。否则,您将泄漏输入流。