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

Discussion in 'Android Questions' started by monki, Jun 19, 2014.

  1. monki

    monki Active Member Licensed 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?

  2. monki

    monki Active Member Licensed User

    no solutionn ?:(
  3. mddmx

    mddmx Member Licensed 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.
  4. monki

    monki Active Member Licensed User

  5. Erel

    Erel Administrator Staff Member Licensed User

    This is a community forum. You shouldn't limit the question to a single member.

    You should create the folder with File.MakeDir. The path is based on the package name.
  6. monki

    monki Active Member Licensed User

    File.MakeDir unfortunately does not work.
    I tested the following variants without success.
    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

  7. Erel

    Erel Administrator Staff Member Licensed User

    There are several problems with your code.

    You know the package name so you can just set it in your code:
    File.MakeDir("/storage/external_sd/Android/data/""b4a/example"'/ instead of .
  8. monki

    monki Active Member Licensed User

    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

    greetings monki
  9. Erel

    Erel Administrator Staff Member Licensed User

    You can use this code on Android 4.4 to find the paths of the app specific external storage paths:
    Sub Activity_Create(FirstTime As Boolean)
    Dim paths() As Object = GetContext.RunMethod("getExternalFilesDirs"Array(Null))
    For Each p As Object In paths
    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)
    Return jo.GetFieldJO("processBA")
    End Sub
    The first path will be the same as File.DirDefaultExternal.
  10. monki

    monki Active Member Licensed 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.

  11. ivan.tellez

    ivan.tellez Active Member Licensed User

  12. Hubert Brandel

    Hubert Brandel Active Member Licensed User

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

    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:

        at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(
        at anywheresoftware.b4j.object.JavaObject.RunMethod(
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(
        at anywheresoftware.b4a.ShellBA.raiseEvent2(
        at b4a.example.main.afterFirstLayout(
        at b4a.example.main.access$
        at b4a.example.main$
        at android.os.Handler.handleCallback(
        at android.os.Handler.dispatchMessage(
        at android.os.Looper.loop(
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(
        at dalvik.system.NativeStart.main(Native Method)
  13. Erel

    Erel Administrator Staff Member Licensed User

    This method is only available on Android 4.4.
  14. Baltazar

    Baltazar Member Licensed 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.
  15. James Chamblin

    James Chamblin Active Member Licensed User

    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.
  16. Baltazar

    Baltazar Member Licensed User

    Great! :)
    Gbenga Odumosu likes this.
  17. kongc

    kongc Member Licensed 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: (
    Gbenga Odumosu likes this.
  18. Erel

    Erel Administrator Staff Member Licensed User

    Change Array to Array As Object .
  19. Filippo

    Filippo Expert Licensed User

    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.
  20. James Chamblin

    James Chamblin Active Member Licensed 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.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice