open_file icon indicating copy to clipboard operation
open_file copied to clipboard

Failed to find configured root that contains ...

Open UnbreakCode opened this issue 1 year ago • 23 comments

used version: 3.3.2 flutter version: 3.10.4 anodrid 12

I cannot open my image with open_file. The app has permission (permission_handler) and I added everything to Manifest.xml. The interesting thing is that I define a different path to the file than specified in the exception. I'm using '/data/user/0/xxx/images/1.jpg'

Seems to be the same issue as https://github.com/crazecoder/open_file/issues/94

E/MethodChannel#open_file(13238): Failed to handle method call
E/MethodChannel#open_file(13238): java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/xxx/images/1.jpg
E/MethodChannel#open_file(13238): 	at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:825)
E/MethodChannel#open_file(13238): 	at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:450)
E/MethodChannel#open_file(13238): 	at com.crazecoder.openfile.OpenFilePlugin.startActivity(OpenFilePlugin.java:170)
E/MethodChannel#open_file(13238): 	at com.crazecoder.openfile.OpenFilePlugin.onMethodCall(OpenFilePlugin.java:109)
E/MethodChannel#open_file(13238): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
E/MethodChannel#open_file(13238): 	at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/MethodChannel#open_file(13238): 	at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/MethodChannel#open_file(13238): 	at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/MethodChannel#open_file(13238): 	at android.os.Handler.handleCallback(Handler.java:938)
E/MethodChannel#open_file(13238): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#open_file(13238): 	at android.os.Looper.loop(Looper.java:223)
E/MethodChannel#open_file(13238): 	at android.app.ActivityThread.main(ActivityThread.java:7656)
E/MethodChannel#open_file(13238): 	at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#open_file(13238): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/MethodChannel#open_file(13238): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

I used this filepaths.xml:

<?xml version="1.0" encoding="utf-8"?> <resources> <external-path name="external_storage_directory" path="." /> </resources>

UnbreakCode avatar Jun 15 '23 15:06 UnbreakCode

Make sure this file is app private directory, then determine whether public directory, if not, you need to usePermission.manageExternalStorage.request()

crazecoder avatar Jun 16 '23 01:06 crazecoder

Make sure this file is app private directory, then determine whether public directory, if not, you need to usePermission.manageExternalStorage.request()

I already did this before opening the file with this path now: /data/user/0/com.example.app/1.jpg

UnbreakCode avatar Jun 16 '23 07:06 UnbreakCode

I'm also having this issue.

The requested directory looks like this:

/data/user/0/com.example.app/app_flutter/files/my_folder/my_file.jpg

I am using path_provider getApplicationDocumentsDirectory to achieve this.

But when using OpenFile, it attempts a directory that looks like this:

/data/data/com.example.app/app_flutter/files/my_folder/my_file.jpg

Is this correct or are we missing something here?

Here is the filepath.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <external-path name="external_storage_directory"
    path="." />
  <external-path name="files"
    path="Android/data/com.example.app/" />
</resources>

Here is the stacktrace:

E/MethodChannel#open_file( 8708): Failed to handle method call
E/MethodChannel#open_file( 8708): java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.app/app_flutter/files/my_folder/my_file.jpg
E/MethodChannel#open_file( 8708): 	at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:825)
E/MethodChannel#open_file( 8708): 	at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:450)
E/MethodChannel#open_file( 8708): 	at com.crazecoder.openfile.OpenFilePlugin.startActivity(OpenFilePlugin.java:170)
E/MethodChannel#open_file( 8708): 	at com.crazecoder.openfile.OpenFilePlugin.onMethodCall(OpenFilePlugin.java:109)
E/MethodChannel#open_file( 8708): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
E/MethodChannel#open_file( 8708): 	at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/MethodChannel#open_file( 8708): 	at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
E/MethodChannel#open_file( 8708): 	at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/MethodChannel#open_file( 8708): 	at android.os.Handler.handleCallback(Handler.java:790)
E/MethodChannel#open_file( 8708): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#open_file( 8708): 	at android.os.Looper.loop(Looper.java:164)
E/MethodChannel#open_file( 8708): 	at android.app.ActivityThread.main(ActivityThread.java:6494)
E/MethodChannel#open_file( 8708): 	at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#open_file( 8708): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
E/MethodChannel#open_file( 8708): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

OpenFile: 3.3.2 Flutter: 3.10.4 Tested on Android 8.1

MJ12358 avatar Jun 17 '23 03:06 MJ12358

I tried it in the example, and it didn't report an error, it just couldn't find the file

crazecoder avatar Jun 20 '23 06:06 crazecoder

I tried it in the example, and it didn't report an error, it just couldn't find the file

but the file is definitely there, I checked it via device explorer

UnbreakCode avatar Jun 20 '23 09:06 UnbreakCode

I've put together a simple test app to recreate this issue. We are creating files within our app's directory, yet OpenFile does not work, permissions aside it continues to fail via: PlatformException(error, Failed to find configured root that contains...).

Tested on Android 7 and 8.1

MJ12358 avatar Jun 24 '23 03:06 MJ12358

same issue.

JosephNK avatar Jun 24 '23 15:06 JosephNK

I don't know why, but when I add the code as below, it works fine.

Hope you can help me in any way.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <root-path name="root" path="."/> <-- added
    <external-files-path name="external_storage_directory" path="." />
</resources>

JosephNK avatar Jun 24 '23 16:06 JosephNK

@JosephNK This works... But I'm concerned with the implications of this. I thought the "root-path" tag allows access to any file or directory within the device's file system. What does this mean for permissions?

Edit: looks like using "READ_EXTERNAL_STORAGE" is all that is necessary for this tag. Edit2: just uploaded an app to Google with "root-path" set and they don't seem to care, so maybe I won't either...

I'm more interested in using "files-path", which should represent the internal storage area specific to your app. In this scenario, "root-path" and "external-files-path" shouldn't even be necessary, correct? This way external storage permissions wouldn't be required.

MJ12358 avatar Jun 26 '23 00:06 MJ12358

Edit2: just uploaded an app to Google with "root-path" set and they don't seem to care, so maybe I won't either...

sometimes they do due to security reasons. But this is still not the real solution for this issue.

EDIT: this fix does not work for me

UnbreakCode avatar Jun 29 '23 09:06 UnbreakCode

But this is still not the real solution for this issue.

I completely agree.

MJ12358 avatar Jul 02 '23 20:07 MJ12358

The same: PlatformException(error, Failed to find configured root that contains /data/data/com.example.farm/app_flutter/ProtocolFileCache/52b62d73-2053-4e67-8a95-8522eb915e88.docx, null, java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.farm/app_flutter/ProtocolFileCache/52b62d73-2053-4e67-8a95-8522eb915e88.docx

but file was: /data/user/0/com.example.farm/app_flutter/ProtocolFileCache/93eaffaf-6635-4e67-87b5-fb807f7deba2.docx

bretbas avatar Jul 05 '23 20:07 bretbas

This is definitely an issue when using internal directories like this:

getApplicationDocumentsDirectory aka /data/user/0/com.example.app/app_flutter vs: getExternalStorageDirectory aka /storage/emulated/0/Android/data/com.example.app/files

When using "OpenFile", the former produces the directory of: /data/data/... (which is incorrect). The latter will correctly open the file.

This issue stems from this line FileProvider.getUriForFile (OpenFilePlugin.java:170).

There must be some association between the "FileProvider" and the resources/paths within "filepaths.xml" that is not working.

MJ12358 avatar Jul 06 '23 02:07 MJ12358

Configuring filepaths.xml with the following works for me when opening files via getApplicationDocumentsDirectory:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <files-path name="files-path"
    path="../" />
</paths>

The important bit is path="../".

I think what is happening is that "files-path" assumes a directory of /data/user/0/com.example.app/files and what we really need is /data/user/0/com.example.app/app_flutter. Therefore we back out of the "files" directory and into our app's root directory.

However, this still seems to require "READ_EXTERNAL_STORAGE". Is this correct?

MJ12358 avatar Jul 06 '23 22:07 MJ12358

Configuring filepaths.xml with the following works for me when opening files via getApplicationDocumentsDirectory:


<?xml version="1.0" encoding="utf-8"?>

<paths xmlns:android="http://schemas.android.com/apk/res/android">

  <files-path name="files-path"

    path="../" />

</paths>

The important bit is path="../".

I think what is happening is that "files-path" assumes a directory of /data/user/0/com.example.app/files and what we really need is /data/user/0/com.example.app/app_flutter.

Therefore we back out of the "files" directory and into our app's root directory.

However, this still seems to require "READ_EXTERNAL_STORAGE". Is this correct?

Exactly, but this requirement is acutally not really needed because it's an app internal storage while runtime

UnbreakCode avatar Jul 07 '23 21:07 UnbreakCode

Just replace your fileprovider.xml or any name you have set for file-provider.xml code with below code

<?xml version="1.0" encoding="utf-8"?>

<paths xmlns:android="http://schemas.android.com/apk/res/android">

  <files-path name="files-path"

    path="../" />

</paths>

praveenverma720 avatar Feb 06 '24 12:02 praveenverma720

This is a question and not a solution. It seems like this is the solution to the problem. However what I don't understand it, where to find the fileprovider.xml in my project. Any help would be appreciated.

<?xml version="1.0" encoding="utf-8"?>

<paths xmlns:android="http://schemas.android.com/apk/res/android">

  <files-path name="files-path"

    path="../" />

</paths>

mzeqirii avatar Feb 14 '24 18:02 mzeqirii

@praveenverma720 If you look at the previous two comments directly above yours, you will see that this has already been suggested. You have not provided any new information. If you have novel information on the permissions issues mentioned above please report. Thanks.

@mzeqirii The location for this file is: android/app/src/main/res/xml. This information is in the readme of this repository.

MJ12358 avatar Feb 15 '24 00:02 MJ12358

same issus ResultType.fileNotFound

datpt11 avatar Mar 23 '24 04:03 datpt11

Same issue

larmaysee avatar Apr 09 '24 04:04 larmaysee

I don't know why, but when I add the code as below, it works fine.

Hope you can help me in any way.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <root-path name="root" path="."/> <-- added
    <external-files-path name="external_storage_directory" path="." />
</resources>

cool.can work

wuweijian1997 avatar Apr 25 '24 10:04 wuweijian1997

I use this configured in working. android\app\src\main\res\xml\filepaths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <files-path name="files-path"
    path="../" />
</paths>

And , Save file use await getTemporaryDirectory(); . Open file example

OpenFile.open('/data/user/0/com.xxxx.android/cache/image.png');

I can also help you.

sherwin avatar Apr 28 '24 08:04 sherwin

Replacing the filepath.xml with this finally worked for me.

<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <files-path name="files-path" path="../" /> </paths>

tanvir-robin avatar Jun 29 '24 15:06 tanvir-robin