0

I cant get the directory , the IDE I am using is Android Studio using Kotlin language, I have inputted the exact path of the image directory which is "D:\Fyp\app\src\main\res\drawable\picture.jpg" , I have tried on IntelliJ with the same codes and it worked as well, but as for on android studio , it didnt work ... Below is the code ...

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
}

and these are the error logs ...

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) 

1 Answer 1

1

It doesn't make sense to use the path of the file on your PC from your Android application.

Also, resources (anything in the res directory of your project) are not traditional files in your compiled application. You can't access them as files, and they might not even exist in the app after it is installed from the store (if they are filtered out based on device characteristics).

To include files in your app that you can load at run time, you need to put them in an assets directory, not in res. If you don't already have an assets directory, create one in src/main. Then put your files in it. You can read it like this:

val bytes = context.assets.open("picture.jpg").use { it.readBytes() }

Note the use of use wrapping the readBytes() call. This is to close the input stream after it's done reading. Otherwise, you leak the input stream.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.