Android Question ExternalStorage and move/rename

koffie

Member
Licensed User
Longtime User
On an Android 12 tablet, I am trying to batch rename JPG's in a Sd-Card folder. I am using the External Storage example which as a base, works to choose the folder on a SD-card and display the JPG files. Besides that I am trying this example to actually move/rename the files:

B4X:
Sub FileMove(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

found here.

The last part does not work.
Looking at the code I need the SourceFolder to start with. I do not know what the appropriate syntax is or where to find it. The only info I get starts with "Content://" which doesn't look right at all.

Besides that I don't even know if I can actually use this code to rename/move files on the Sd-card with all the file access permission changes of the last year in mind. There are dozens of files that are about 20MB a piece. Copy and delete isn't really an option.

Can somebody help.

Regards
 

DonManfred

Expert
Licensed User
Longtime User
Dim source, target As JavaObject source.InitializeNewInstance("java.io.File", Array(SourceFolder, SourceFile)) target.InitializeNewInstance("java.io.File", Array(TargetFolder, TargetFile))
that will not work. You are building a File-Object whereas you need to get an ExternalStorage-Reference.
You need to run the renameTo on this reference then.


May be helpful to find the file to rename.
use renameTo on the ExternalFile-Reference....

Look at the externalstorage class source to understand

B4X:
'Creates a new directory
Public Sub FindDirOrCreate(Parent As ExternalFile, Name As String) As ExternalFile
    Dim f As ExternalFile = FindFile(Parent, Name)
    If f.IsInitialized = False Then
        Return DocumentFileToExternalFile(Parent.Native.RunMethod("createDirectory", Array(Name)))
    Else
        Return f
    End If
End Sub
 
Upvote 0

koffie

Member
Licensed User
Longtime User
Thanks for the quick response. With your answer i know i have a long way to go, to call myself knowledgable about B4A ?.
I will take your answer sentence by sentence and try to understand it.
Thanks again
 
Upvote 0
Top