Android Question how to ext. SD Card write on Android 4.4.

monki

Active Member
Licensed User
Longtime User
hi erel,
in Android 4.4 it is only possible to write to the external SD card in a directory whose name Andoid / data / "apk name".
But as it is created?

monki.
 

mddmx

Member
Licensed User
Longtime User
Are you sure about that? I am running 4.4.3 and have had no problems with writing to a folder created off the root of the SD Card.
 
Upvote 0

monki

Active Member
Licensed User
Longtime User
File.MakeDir unfortunately does not work.
I tested the following variants without success.
B4X:
Dim r As Reflector
  dim pname as string = GetStaticField ("anywheresoftware.b4a.BA", "package name")

File.MakeDir ("/storage/external_sd", pname) 'no Dir created, No Error log
File.MakeDir ("/storage/external_sd/Android/data/", pname)' no Dir created, No Error log

it works with the internal Sd-Card

monki
 
Upvote 0

monki

Active Member
Licensed User
Longtime User
erel
my code works with the internal sd card and also with the external sd card on Android 4.3.
under kitkat it work only with the internal sd card.
the missing slash in the first File.makedir line is a copy error it exists in the real code.
I have tested your code, it will not create folders under kitkat on the external sd card
I've seen the apps that use the external sd card under kitkat folder names designate their just like the apk name so with point.

Examples of some folders from my lg phone
proofit.klack.phone
com.lge.lgworldcom.android.gallery3d

greetings monki
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code on Android 4.4 to find the paths of the app specific external storage paths:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim paths() As Object = GetContext.RunMethod("getExternalFilesDirs", Array(Null))
   For Each p As Object In paths
     Log(p)
   Next
End Sub

Sub GetContext As JavaObject
  Return GetBA.GetField("context")
End Sub

Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")
End Sub

The first path will be the same as File.DirDefaultExternal.
 
Upvote 0

monki

Active Member
Licensed User
Longtime User
Thank you erel for your quick response.

your code works great it creates the necessary folders on the external sd card if it does not exist. (its fine forCode Snippets)
the line "For Each p As Object In paths" gave me no output in the log, I had change in For Each p As String In paths .
Once again many thanks.

monki
 
Upvote 0

Hubert Brandel

Active Member
Licensed User
Longtime User
You can use this code on Android 4.4 to find the paths of the app specific external storage paths:
B4X:
Sub Activity_Create(FirstTime As Boolean)
  Log("file.DirInternal  " &  File.DirInternal )
   Log("File.DirDefaultExternal  " &  File.DirDefaultExternal )
  Log("File.DirRootExternal  " &  File.DirRootExternal )   
   Log("File.ExternalWritable  " &  File.ExternalWritable )
 
   Dim paths() As Object = GetContext.RunMethod("getExternalFilesDirs", Array(Null))
   For Each p As String In paths
     Log(p)
   Next
End Sub

Sub GetContext As JavaObject
  Return GetBA.GetField("context")
End Sub

Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA") ***** ERROR LINE 64
End Sub

The first path will be the same as File.DirDefaultExternal.

When I start this code on my Galaxy S+ (Android 2.3.3) I get this error

B4X:
file.DirInternal        /data/data/b4a.example/files
File.DirDefaultExternal  /mnt/sdcard/Android/data/b4a.example/files
File.DirRootExternal    /mnt/sdcard
File.ExternalWritable    true

Error occurred on line: 64 (main)
java.lang.RuntimeException: Method: getExternalFilesDirs not found in: android.app.Application

    at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:346)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:97)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
    at b4a.example.main.afterFirstLayout(main.java:98)
    at b4a.example.main.access$100(main.java:16)
    at b4a.example.main$WaitForLayout.run(main.java:76)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    at dalvik.system.NativeStart.main(Native Method)
 
Upvote 0

Baltazar

Member
Licensed User
Longtime User
From what I have gathered, there is a confusion about this news. Just like what Erel has mentioned, only one folder will be allowed in the external SD card and that is for every application. This is just kinda imposing some discipline in the file system usage. If one needs more folder for his/her external files then it has to be created inside that lone folder or the default storage ( the device flash storage). Another info that I have heard is that ownership is implemented in this scheme hence, from KitKat and future Android releases, there will be no sharing of folders among applications. I just thought , could it mean that if your application wrote some audio files in its external folder then other apps like third party media player would not be able to play those files? No wonder it is not a welcome idea to millions of Android fans. But again, this is only for external SD card.
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
From what I have gathered, there is a confusion about this news. Just like what Erel has mentioned, only one folder will be allowed in the external SD card and that is for every application. This is just kinda imposing some discipline in the file system usage. If one needs more folder for his/her external files then it has to be created inside that lone folder or the default storage ( the device flash storage). Another info that I have heard is that ownership is implemented in this scheme hence, from KitKat and future Android releases, there will be no sharing of folders among applications. I just thought , could it mean that if your application wrote some audio files in its external folder then other apps like third party media player would not be able to play those files? No wonder it is not a welcome idea to millions of Android fans. But again, this is only for external SD card.
As I understand it, the restriction is only on writing files. You can still read files created from other apps, so media player can still play your audio file.
 
Upvote 0

kongc

Member
Licensed User
Longtime User
Hi Erel,

I got error when running the code at post #9. What's wrong?

Parsing code. 0.00
Compiling code. Error
Error compiling program.
Error description: 'as' expected.
Occurred on line: 28
Dim paths() As Object = GetContext.RunMethod("getExternalFilesDirs", Array(Null))
Word: (
 
Upvote 0
D

Deleted member 103

Guest
Hi,
how can, for example, the app ES Datei Explorer on the SD-Card save and delete new files and folders?
The app is not a system-app, it has special permission? If yes, which ones?
My app uses the SD-card for backups.
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
ES explorer uses a hack to bypass the SD card restriction. As this hack is not sanctioned by Google, there is a risk that it will not work in future versions.
 
Upvote 0
Top