Android Question [CLOSED] Read/Write/CreateDir DirRootExternal

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

With the up coming changes to accessing "External Memory" I am endeavouring to get "requestLegacyExternalStorage " out of the manifest and still be able to access DirRootExternal.
My Apps require this function to make data available to the user and import data from the user.

I have read Erel's "B4A Class ExternalStorage - Access SD cards and USB sticks" post, Googles document and other stuff. It all seems to be aimed at actual external memory [USB sticks Etc.], not the internal memory that Android designates as external memory. Having tried to make sense of all this, there appears to be a number of options that don't seem to fit with what I need.

Below is the manifest of one of the apps and part of the code where DirRootExternal is accessed. Is Erel's Class ExternalStorage the way to go or is there something else I've missed?

'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="29"/>

<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>

)

SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

CreateResourceFromFile(Macro, Core.NetworkClearText) 'Added 11/7/19
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
SetActivityAttribute(HelpActiv, android:screenOrientation, "landscape")

'Used to recreate IDisplay with decent cursor
CreateResource(layout, layout1.xml,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:eek:rientation="vertical" >

<EditText android:tag="EditText1" android:id="@+id/edittext1"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:textCursorDrawable="@drawable/my_custom_cursor_drawable"
/>
</LinearLayout>
)
CreateResource(drawable, my_custom_cursor_drawable.xml ,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<size android:width="3dp" />
<solid android:color="#FFFCFCFC"/>
</shape>
)

AddPermission(android.permission.ACCESS_NETWORK_STATE)

'SetApplicationAttribute(android:requestLegacyExternalStorage, true)


Below is a small part of the Activity_Create code that Makes a Dir if not already there and copies the HelpFile to this Dir. From here the user can download/print/forward the file as required.
Elsewhere in the App the user save a text file to this Dir where the App can import the file which is used to program a new function for the App.

Part of Activity_Create:
Sub Activity_Create(FirstTime As Boolean)

'Lots of code ....................

'Permission to READ/WRITE External Memory       
        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        '....  and then
        If Result Then     'The HepFile is copied to an external directory where it can transferred/printed etc.
'Copy Help file to the C_Calc folder
            If File.Exists(File.DirRootExternal&"/C_Calc/", "") = False Then
Log("No C_Calc")
                File.MakeDir(File.DirRootExternal&"/C_Calc/", "")       
If File.Exists(File.DirRootExternal&"/C_Calc/", "") = False Then Log("Still no C_Calc")

            End If
            File.Copy(File.DirAssets,"helpfile.htm",File.DirRootExternal&"/C_Calc/","helpfile.htm")     'This is where the program crashes
        End If   

'More code............................


If anyone point me in a direction or a simple example I would appreciate it.
Sorry if this seems vague but this is as good a question I can form at this point.
Google seems intent in driving hobbyists out of the Android App arena. First Maps APIKeys, now your App can't even read a text file in the download folder.

Regards Roger
 

agraham

Expert
Licensed User
Longtime User
I have read Erel's "B4A Class ExternalStorage - Access SD cards and USB sticks" post, Googles document and other stuff. It all seems to be aimed at actual external memory [USB sticks Etc.], not the internal memory
It works fine with internal memory, see Erel's note 2. I use ExternalStorage in my mapping app so the maps may be placed either on an SD Card or in the internal memory for devices that have lots of it and/or lack an SD Card slot.

Is Erel's Class ExternalStorage the way to go or is there something else I've missed?
ExternalStorage is the only way to go for apps that will be in the Play Store. SDK 30 and later will have a new MANAGE_EXTERNAL_STORAGE permission but this will only be allowed in store apps that need it.

Manage all files on a storage device | Android Developers
 
Upvote 0
Top