Android Question External Sd card access

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to All
I have now met the problem to manage an External sd card. Reading the posts here I found a Class "ManageExternalStorage" by AGraham, and an "ExternalStorage" by Erel. The second allows me to navigate and get access to the Sd folder and see the files that I need to read. BUT, as long as I try to "hack" the simple thing that I need, namely how to read the files, I discover that all the mechanism is inside the Lib, and for me is useless because, in my case, my App must automatically read the files, not ask the User to do it (btw the files are several thousands..). The first AGraham's App, initially, displayed a message asking : "This app requires access to all files, please enable the option" (Image1). Answering Yes, it displays anyway a list of Internal Android Directories, like in Image2. No access to external Sd storage. To be sure to enable the required permissions, I changed the default name of the App, to "ExStor". Changed also in AGraham's code (project attached). Then I found where to allow all storage permissions for ExStor in Android definitions and allowed all. As a matter of fact now the App doesn't display the above mentioned message .. but .. no Sd card access anyway.
At the end, what I need is to access thousands of files from the Sd card. At the moment, I manage them in DirDefaultExternl (ok .. GetSafeDirExternal.. of course). I dont' have to copy them or see them or whatever else. Just read from my App when necessary. The use of an external Sd card is justified, besides the number and global dimension of the files, from the fact that it is much faster to load them on a computer and after put the loaded Sd card on the tablet, which may be elsewhere.
Any suggestion? Thanks in advance.
 

Attachments

  • ExStor.zip
    13.5 KB · Views: 169
  • image1.png
    image1.png
    31.2 KB · Views: 149
  • image2.png
    image2.png
    73.3 KB · Views: 145

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
The standard way to achieve it is with ExternalStorage, which requires the user to select the folder once.
Ok. Thanks, this is clear. But "access the folder once" with what? What means "access"? Perhaps there is some misunderstanding and I don't figure out what I have to do. In this specific case, I accessed the folder with your ExternalStorage example. I saw the files. Just seen, not did anything else. I loaded them by cable, according to my habit. Nevertheless the AGraham's App still behaves same way, that is no access to the ExternalStorage. Shall I use the Samsung File Manager or what else? Thanks again.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. I didn't write "access the folder once". I wrote "which requires the user to select the folder once".
2. There is no relation between ExternalStorage and special MANAGE_EXTERNAL_STORAGE permission (I think that you are referring to this).

Nevertheless the AGraham's App still behaves same way, that is no access to the ExternalStorage.
Not sure. I'm not familiar with this.

Shall I use the Samsung File Manager or what else?
Not if you want to directly access all files.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The ManageExternalStorage library is intended to allow apps to access almost the entire internal file store, Confusingly this is what Gogle calls the permission for this as originally this was the term used to differentiate it from the other internal storage that Android itself uses and is not accessible to other than rooted devices.

The ExternalStorage library is what you need to access the SD Card. As Erel states once the user has manually selected the root of the SD Card file system using SelectDir then your app can roam free across that file system without further user intervention. However you will need to use the methods of the ExternalStorage library to do this. If you don't know the structure of the storage you can recursively build it using ListFiles and check if each file is a directory by checking the IsFolder parameter of each ExternalFile. You can only read or write files by using streams and OpenInputFile and OpenOutputFile in conjunction with the File methods that work with streams.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Moreover, in what I see, ExternalStorage is a tool for managing the files, and it works, as I see. But gives no information or chance to resolve my situation. My app loads thousands of files to manage a bigtiff OrthoPhoto. Due to its dimensions, I have divided it in Tiles. The App dynamically reads and displays only the tiles that are interested by the actual zoom. Putting the tiles to the Tablet (unluckily with usb 2) takes hours because they are from 5000 to 10000, for a total of 30 or 40 Gb. Tblet has 64 Gb disk, so there are "no problems". But te idea was to manage this not on the Android/data/App/files folder (DirDefaultExternal) but on the External Sd. In this way we can write the Tiles on the Sd directly on the computer and, besides speed of writing, we can do it without the Tablet, which is elsewhere. I have the doubt that accessing the external Sd dynamically during App execution could be much slower that doing the same on the DirDefaultExternal. This drawback should be a lucky circumstance, allowing to discard the idea of managing the Tiles on the external Sd, which seems, at this point, not so easy.. Thanks for attention.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Sorry, but from the post above I have no idea what your problem is with using ExternalStorage. What is it that you want to do but can't?
I just need to read the files on the Sd. My files as bitmaps. Actually I read them with bm.Initialize(dir, filename). To get the "dir" I first allow the user to select one directory from the list of directories which are inside "Android/Data/MyApp.xxx/files". Let's say that dir is "/storage/emulated/0/Android/data/MyApp.xxx/files/DirName", that is "GetSafeDirDefaultExternal/etc.". I would like to have "bm.Initialize(Folder_On_SdCard,filename).
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Sorry, but from the post above I have no idea what your problem is with using ExternalStorage. What is it that you want to do but can't?
Maybe the confusion is due to the words used. Actually I use DefaultExternal. I was meaning, instead, the additional Sd card that I have put in the tablet.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I know you meant the SD Card. That's why you need to use the ExternalStorage library. You seem to think that won't work for you but I fail to understand why you think that, particularly as all you want to do is to read the file. You should be able to use Bitmap.Initialize2 to read a bitmap from a file opened on an SD Card input stream.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
I did a first step, doing an App which uses ExternalStorage lib. I began with the following code, which is the basic B4Xpages template, with addition of ExternalStorage lib.
After initializing the object, in practice I have just an instruction "SelectDir". As a first choice, i do "SelectDir(False). This allows to navigate the Directories in the Tablet, including the Sd card. After selecting the directory I want, I get a crash. Of course if I put SelectDir(true) i get the crash with no previous navigation dialog.
I attach the log file. thanks for attention.
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private ExtStorage As ExternalStorage
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

Private Sub Button1_Click
    Dim s As String
  
    ExtStorage.Initialize (Me, "ExtStorage")
    
    s=ExtStorage.SelectDir(False) 
    
    Log(s)    
    
End Sub
 

Attachments

  • log.txt
    2.1 KB · Views: 187
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
I know you meant the SD Card. That's why you need to use the ExternalStorage library. You seem to think that won't work for you but I fail to understand why you think that, particularly as all you want to do is to read the file. You should be able to use Bitmap.Initialize2 to read a bitmap from a file opened on an SD Card input stream.
Sorry, great Agraham. I just get problems ... as you see from my picture, I mostly lost my hair .. do you really think that I am just paranoic or "maybe" there is something strange? I prefer not to do any polemic on "access" or "select".. etc. this I learnt losing hair .. Of course,l i could be totally idiot .,, as usually I think of myself .. no surprise .. Thanks a lot for your patience..
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Your problem is

java.lang.ClassNotFoundException: androidx$documentfile$provider$DocumentFile

For some reason the ExternalStorage library can't find the Java DocumentFile class. I'm afraid that I haven't used ExternalStorage myself for years as all my current devices don't have SD Card slots so I can't suggest why this class is missing. Maybe someone else can suggest something.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Your problem is

java.lang.ClassNotFoundException: androidx$documentfile$provider$DocumentFile

For some reason the ExternalStorage library can't find the Java DocumentFile class. I'm afraid that I haven't used ExternalStorage myself for years as all my current devices don't have SD Card slots so I can't suggest why this class is missing. Maybe someone else can suggest something.
Well. The progress I did is that, putting a stupid Sleep(1000) after initialization, I have no crash with SelectDir(False) (with true it happens anyway). Anyway, the Log of the s=SelectDir(false) shows an empty string. etc. etc.. Thanks.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Anyway, if my system were missing something, as the debug says:

java.lang.ClassNotFoundException: androidx$documentfile$provider$DocumentFile

Erel's App using ExternalStorage should not working. Instead it works.. Finally I did same Manifest of Erel's example. No change. Nothing else to say from my side.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to all.
After all this coloured posts, here is the solution, (I guess):
it is necessary to put this line at the beginning of the App:

#AdditionalJar: androidx.legacy:legacy-support-core-utils

That's all. I hope..

Thanks for your patience...
 
Upvote 0
Top