Answer the question
In order to leave comments, you need to log in
How to access a file from gallery?
Hi all!
I am making an application in which there is a module with a choice of a file from the gallery (and indeed, any file from the device).
The selection is successful. But then, when I want to access this file, then problems begin - the android does not like the path by which I access this file.
Everything seems to be just as it should be, but I'm confused with these fucking ways.
compileSdkVersion 28
minSdkVersion 15
targetSdkVersion 28
Code:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
FILE_SELECT_CODE ->
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
var uri = data?.data
Log.d(javaClass.simpleName, "File Uri: " + uri.toString()) // => File Uri: content://com.android.providers.media.documents/document/image%3A213701
var fileInputStream1 = FileInputStream(File("/sdcard/MyApp/my_file.jpg")) //если задать путь так, то всё работает
Log.d(javaClass.simpleName, "fileInputStream1: $fileInputStream1") // => fileInputStream1: [email protected]7a92a
var fileInputStream2 = FileInputStream(File(uri?.path)) // <- в этой строке падает
Log.d(javaClass.simpleName, "fileInputStream2: $fileInputStream2")
}
}
super.onActivityResult(requestCode, resultCode, data)
}
File Uri: content://com.android.providers.media.documents/document/image%3A213701
fileInputStream1: [email protected]
27101-27101/com.my_app.android E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.my_app.android, PID: 27101
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:213701 flg=0x1 }} to activity {com.my_app.android/com.my_app.android.ChatActivity}: java.io.FileNotFoundException: /document/image:213701 (No such file or directory)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4398)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4440)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.io.FileNotFoundException: /document/image:213701 (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:231)
at java.io.FileInputStream.<init>(FileInputStream.java:165)
at com.my_app.android.ChatActivity.onActivityResult(ChatActivity.kt:611)
at android.app.Activity.dispatchActivityResult(Activity.java:7462)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4391)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4440)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Answer the question
In order to leave comments, you need to log in
Yes, this path is not real. This has to do with FileProviders and uri permissions. You need to use ContentResolver.openInputStream .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question