Android Question DirInternal saving images problem

zmrcic

Member
Licensed User
Hi...
at the start of my app I ask for permissions

B4X:
Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        File.MakeDir(File.DirInternal,"HG")
       
        If File.Exists(File.DirInternal & "/HG", "2.swa") Then
            Try
                sCode=File.ReadString(File.DirInternal & "/HG", "2.swa")
            Catch
                Log(LastException)
                ToastMessageShow("File error",False)
            End Try
       
        End If
   
    End If


no error there, I know because 2.swa is just txt file where I store some data. I can read it later.
B4X:
Dim In As InputStream
In.InitializeFromBytesArray( bSlika, 0, bPic.Length)
Dim bmp As Bitmap
bmp.Initialize2(In)

Dim out As OutputStream
out = File.OpenOutput(File.DirInternal & "/HG", "1.jpg", False)
bmp.WriteToStream (out, 100, "JPEG")
out.Close

but when I try to rename it using
B4X:
Sub FileRename(SourceFolder As String, SourceFile As String, TargetFolder As String, TargetFile As String) As Boolean
    Dim source, target As JavaObject
    source.InitializeNewInstance("java.io.File", Array(SourceFolder, SourceFile))
    target.InitializeNewInstance("java.io.File", Array(TargetFolder, TargetFile))
    Return source.RunMethod("renameTo", Array(target))
End Sub

rename returns false.

It looks like image is saved. Tried to browse all my folders, I know I can not find DirInternal. Can it be that I need some more permissions?
On my Huawei RNEL21 code working OK, but have problems with some other devices. And also, are files stored in DirInternal there forever, is that folder something like cash?
thanks
 
Last edited:

Brian Dean

Active Member
Licensed User
A common way to rename a file (unless file size is a problem) is to use File.Copy to make a copy with the new name, and then File.Delete to remove the original. This seems simpler to me, and stays within B4X.
... are files stored in DirInternal there forever,
Files in DirInternal remain until the app is uninstalled. They persist over app updates. It acts just as any normal folder.
 
Upvote 0

Alessandro71

Active Member
Licensed User
A common way to rename a file (unless file size is a problem) is to use File.Copy to make a copy with the new name, and then File.Delete to remove the original. This seems simpler to me, and stays within B4X.
This just makes the lack of a File.Rename command even more outstanding :)
 
Upvote 0
Top