Android Question Uri to absolute file path

I want to get the path of the file in the storage associated with my app as soon as the user clicks on the file and it opens with my app. But the path returned to the app is in Uri format. I want it in absolute file path for further operations. I am using the code given by Mr. Erel at the link given Here.

But when I run the project I get the following error -
Error on android.support.annotation.NonNull:
B4A Version: 10.0
Java Version: 11
Parsing code.    (0.16s)
Building folders structure.    (0.08s)
Running custom action.    (0.24s)
Compiling code.    (0.38s)
Compiling layouts code.    (0.01s)
Organizing libraries.    (0.00s)
    (AndroidX SDK)
Generating R file.    (0.03s)
Compiling debugger engine code.    (0.07s)
Compiling generated Java code.    Error
src\com\proximate\test\main.java:16: error: package android.support.annotation does not exist
import android.support.annotation.NonNull;
                                 ^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

javac 11.0.1

First I compiled with Android SDK then I got the issue. I thought the issue might be related to the Android SDK hence I downloaded all the recommended items in the SDK Manager and compiled with AndroidX SDK but the issue remains the same.

I commented out different import lines then I found that the error is raised on following imports in the code -
B4X:
import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;

Please suggest solution.
 
There is an Import File button in my app. Upon clicking on the button it opens File Chooser which shows files associated with my app in the storage. User clicks on the file and then the file is opened in my app for further processes like reading and writing.

I want the user to open file with my app even if he clicks file in the storage. I have added following code to the manifest editor which shows my app icon in the Open With panel when the user clicks on the file.
B4X:
AddActivityText(Main,
<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.EDIT" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="file" />
    <data android:scheme="content" />
    <data android:mimeType="*/*" />
    <data android:host="*" />
    <data android:pathPattern=".*\\.ext"/>
</intent-filter>
)

where .ext is the extension associated with my app. Through this I get the path of the file in uri format but I can't process the file further as I need the absolute file path. Therefore I need a solution to convert Uri path into absolute or local file path.
 
Upvote 0
I tried using ContentChooser.
To select the custom file with the extension ".ext" I added the code in manifest as I mentioned above -
AddActivityText(Main,
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" />
<data android:scheme="content" />
<data android:mimeType="*/*" />
<data android:host="*" />
<data android:pathPattern=".*\\.ext"/>
</intent-filter>
)

Then I use the following code in the app -
Custom file Mime type in android app:
Dim CC As ContentChooser
CC.Initialize("CC")
CC.Show("application/octet-stream", "Choose File")

This certainly opens the ContentChooser and shows files with ".ext" extension but upon clicking on it I get the following values from Dir and FileName parameters of CC_Result sub -
For Dir - ContentDir
For FileName - content://com.android.providers.downloads.documents/document/226856
So the FileName I get is certainly in Uri format. Moreover this makes it difficult to process file for further operations as I cannot evaluate the file name in the Uri.

But Interestingly when I tried using selecting images with following code -
Custom file Mime type in android app:
Dim CC As ContentChooser
CC.Initialize("CC")
CC.Show("image/*", "Choose File")

I get the following path from Dir and FileName parameters of CC_Result sub -
For Dir - ContentDir
For FileName - content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2FIMG_20201129_132241.jpg

If these parameters are used in code like -
B4X:
ImageView1.Bitmap = LoadBitmap(Dir, FileName)
the code correctly renders the image in Image View.
I wonder if there is some function which loads custom files in same way.
 
Last edited:
Upvote 0
Top