Android Question location of String.txt at internal storage

rogel a. tolentino

Member
Licensed User
B4X:
File.WriteString(File.DirInternal, "String.txt", "This is some string" & CRLF & "and this is another one.")
B4X:
Msgbox(File.ReadString(File.Dirinternal, "String.txt"), "string.txt")

the above codes are succesful and I already view the data of string.txt using msgbox.
I tried to find string.txt at the internal storage using file manager but failed.
Where can I view the said file?
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
Can I read a text file located at the root of internal storage or any folder of the internal stoage like in EXternal sd using File.DirRootExternal?

Yes - You were told this yesterday, but you have asked this question three times in different threads so no wonder you are still confused.

Files that you write using "File.DirExternal" will be stored in the 'public' space of your 'phone. You will find them in the folder ...

/sdcard/Android/data/<package name>/files

Files that you write using "File.DirInternal" are "internal to the app", so are not accessible to other apps or to you, unless you have rooted your 'phone.

I hope that this is clear. "External/Internal" relate to the app, not to physical 'phone memory.
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Okay - Maybe you need an example ...

B4X:
Sub Process_Globals

End Sub

Sub Globals
  
   Dim lstTest As ListView

End Sub

Sub Activity_Create(FirstTime As Boolean)
   addViews
   WriteFiles
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub addViews
   lstTest.Initialize("")
   Activity.AddView(lstTest, 0dip, 0dip, Activity.Width, Activity.Height)
End Sub

Sub WriteFiles
   ' This file will be written to the app private storage
   File.WriteString(File.DirInternal, "File One", "This is the data")
   lstTest.AddSingleLine("Wrote file to private storage")
   ' This file will be written to the app public storage.
   ' It will be in sdcard/Android/data/<Package name>/files/
   ' The User will have to give WriteExternalStorage permission
   File.WriteString(File.DirDefaultExternal, "File Two", "This is the data")
   lstTest.AddSingleLine("Wrote file to public storage")
   ' This file will be written to the sdcard root folder.
   ' Again, the User will have to give permission
   File.WriteString(File.DirRootExternal, "File Three", "This is the data")
   lstTest.AddSingleLine("Wrote file to sdcard root")
End Sub

If you do not understand this, or if I have misunderstood your question, please reply in this thread. Do not start yet another new thread.
 
Last edited:
Upvote 0

rogel a. tolentino

Member
Licensed User
You mentioned
[Files that you write using "File.DirInternal" are "internal to the app", so are not accessible to other apps or to you,
unless you have rooted your 'phone.]

what do you mean rooted the phone?
 
Upvote 0

rogel a. tolentino

Member
Licensed User
I already understand DirInternal, DIrRootExternal and DIrDefaultExternal.
My goal is to read/write a file in the internal memory storage either in root or folder.which can be view in public.
How can I? Is rooting the phone a solution?
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
My goal is to read/write a file in the internal memory storage either in root or folder.which can be view in public.

You have probably already done this without realising it. Let us say that you want to write a file called "MyFile Three" in a folder called "My Folder" in the root of your 'phone's storage. Here is the code ...

B4X:
    File.MakeDir(File.DirRootExternal, "My Folder")
    File.WriteString(File.DirRootExternal, "My Folder/MyFile Three", "This is the data")

The first statement creates the folder. You only need to do this once (obviously).

I already understand DirInternal, DIrRootExternal and DIrDefaultExternal.
My goal is to read/write a file in the internal memory storage either in root or folder.which can be view in public.

I don't think that the first sentence is quite true, or you would not have written the second sentence in the way that you have. But lets see if this works and come back if you have any trouble.

Two more things. Forget about "rooting the 'phone" - that is something else entirely. And there is a book about B4A called "Rapid Android App Development using BASIC." It is in English but it is excellent, and I recommend it if you are going to use B4A (which is also excellent).
 
Upvote 0

rogel a. tolentino

Member
Licensed User
Your example is for external sd card removable from phone. What I want is to read/write in the internal memory of a phone which can be viewed in public.
The DirInternal is not for public. Thanks for the patience and for understanding of my level in b4a.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Your example is for external sd card removable from phone. What I want is to read/write in the internal memory of a phone which can be viewed in public.
No, that is not true. Brian's example is for the internal part of the phone/tablet that is accessible to public but referred to as External. It is not the removable sd card. As Brian mentioned, you can access it via File.DirRootExternal or rp.GetSafeDirDefaultExternal("") when you use a target of 23 and higher in the manifest. You need runtimePermissions library to get rp.
But if you use File.DirRootExternal or File.DirDefaultExternal without runtime permissions, make sure to use a target SDK in manifest of 22 or lower. It is not recommended not use runtime permissions these days.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Your example is for external sd card removable from phone.

No, it is not. That is exactly why I felt that you have not really understood "internal" and "external" storage. This is a very common misunderstanding, because internal and external mean different things to different people at different times. Don't worry - it isn't just you. The reply above is correct, but just in case I am going to try and explain again as well.

All of the memory we are talking about is in the memory chip(s) in your 'phone. Forget about sdcards. For "internal" think "private"; for "external" think "public". When you use what Android calls "DirInternal" it means memory that is private to the app - other apps cannot see it. When it says "Dir...External" it means memory that is accessible to others - if you have a file manager app then you will be able to see it.

User Permissions are a recent complication, but I should forget about them for the time being. Let's just solve these problems one step at a time. Try the code that I have shown you.

[Edit : As Mahares has pointed out (below), that last paragraph is incorrect, and we cannot keep it that simple unless you are working with an old Android level. You probably will have to worry about run time permissions.

Here is an update to my earlier example that includes runtime permissions ...

B4X:
Sub Process_Globals
    Private rp As RuntimePermissions
End Sub

Sub Globals
     Dim lstTest As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    addViews
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    If Permission = rp.PERMISSION_WRITE_EXTERNAL_STORAGE Then
        WriteFiles
    End If
End Sub

Sub addViews
    lstTest.Initialize("")
    Activity.AddView(lstTest, 0dip, 0dip, Activity.Width, Activity.Height)
End Sub

Sub WriteFiles
    File.MakeDir(File.DirRootExternal, "My Folder")
    File.WriteString(File.DirRootExternal, "My Folder/MyFile Three", "This is the data")
    lstTest.AddSingleLine("File written to storage")
End Sub

You will have to tick "RuntimePermissions" in the Libraries tab. Search for "Runtime Permissions" in the Search box - there is a good tutorial there.]
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
but I should forget about them for the time being. Let's just solve these problems one step at a time. Try the code that I have shown you.
The code you have shown him will fail right off the box, because he most likely start with a project from scratch, and all new projects default to Target SDK of 26 in the manifest. Unless you instruct him to lower that SDK number before execution or use RunTime Permissions, it will crash. I understand you are trying to make it simpler for him, but it can back fire.
 
Upvote 0

rogel a. tolentino

Member
Licensed User
B4X:
Sub Process_Globals
    Private rp As RuntimePermissions
End Sub

When I compile the list below appear with error. It happens when i encode private rp as RunTimePermissions

B4A Version: 8.50
Parsing code. (0.00s)
Building folders structure. (0.01s)
Compiling code. (0.01s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.00s)
Generating R file. Error
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:fontVariationSettings
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:ttcIndex
 
Upvote 0

rogel a. tolentino

Member
Licensed User
B4X:
B4A Version: 8.50
Parsing code.    (0.00s)
Building folders structure.    (0.01s)
Compiling code.    (0.02s)
Compiling layouts code.    (0.00s)
Organizing libraries.    (0.00s)
Generating R file.    Error
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:fontVariationSettings
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:ttcIndex
The abobe error apper when compiling
 
Upvote 0

rogel a. tolentino

Member
Licensed User
B4X:
'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="26"/>
<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, Themes.DarkTheme)
'End of default text.

The above code is the content at manifest editor for your additional info
 
Upvote 0

rogel a. tolentino

Member
Licensed User
Additional obserbation: I thought runtimepermission in the library has a problem. when i remove this in the library the below error did not appear

Generating R file. Error
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:fontVariationSettings
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:ttcIndex
 
Upvote 0

rogel a. tolentino

Member
Licensed User
Thank you very much for all the patience and full support. I can read and write a file already. Hope that you will continue your support if i have some querries in the future
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Thank you very much for all the patience and full support

You will find plenty of people happy to help you on this forum. When you have been helped it is good to give a 'like'.

Good luck.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…