Sometimes I may encounter the need to modify the file format in batches, but Baidu has not found the relevant application for a long time, so I want to learn Android for two days to get an APP to meet this demand.

1. Use shell scripts to change file formats and file names in batches

The first method was my first success, since Android itself is a Linux adaptation, so using a little shell command makes sense, right? And it should work regardless of any version of Android (like Android 1-11), so here’s a quick look at it.

Shell is the command line. Generally speaking, in Linux, we can use mv oldFileName newFileName to change the file name (including the file format) for a single file. If you want to change the file name in batches, then you can add a loop, and the code is as follows

#Iterate over all.mp4 files
for name in *.mp4
do
#Change the file format and give it a name
mv "$name" "${name%.mp4}.myvideo"
done
Copy the code

Then change the suffix of the text file to.sh, and find an application that can execute the.sh (script file) or open the terminal (chmod 777 script.sh) to execute the code. I am using MT Manager to execute (coolan inside can be next, coolan -> MT manager)

Talk about the pros and cons of this approach

  1. Advantages, does not involve too much code knowledge, the shell script only has a for loop, the code is easy to modify to meet the corresponding requirements of the code, can be said to be zero burden to solve the requirements, and different versions of Android as long as the script file can be executed to do batch modification, strong compatibility
  2. The downside, obviously, is that you need to download the APP (you don’t know if the APP is risky, there may be ads, and no one will write an APP specifically for batch files unless you develop it yourself).

2. Use the DocumentFile to modify the file format or file name in batches

This method is in line with the topic of the article method, write an APP to achieve this demand.

As mentioned earlier, script files were my first success. Up until then, I had been trying to implement batch files in Java, which was possible with most tutorials on the web before Android 10(API 29), but Android 10 enabled partitioned storage, Each app can only access its own space and a few external shared storage Spaces specified by Android (for example, media content in albums, files under Download folders).

The normal way of using File#renameTo() won’t work on android 10, and you’ll need permissions to manipulate files (you can still modify files created by other apps, and your own apps).

At first I just want to modify the format for video file, according to the introduction of media files within android official document update (link), we need to throw the capture platform RecoverableSecurityException to users consent to modify the file, it’s not just in our run counter to the title of the article? If I want to apply for permission one by one, I might as well use the local file manager to rename the file one by one, or change the file format/file name

Away from the official media file update, the method of using the official document give another plan, access to documents and other files from the Shared storage space, users only need to grant a batch file directory access permissions, you can to make any changes to your files in the directory, with ideas can write the corresponding logic, the code is as follows

.ACTION_OPEN_DOCUMENT_TREE Intent = ACTION_OPEN_DOCUMENT_TREE Intent = ACTION_OPEN_DOCUMENT_TREE Intent = ACTION_OPEN_DOCUMENT_TREE
public void getPermission(a) {
    Intent intent = newIntent(Intent.ACTION_OPEN_DOCUMENT_TREE); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivityForResult(intent, STORAGE_PERMISSION_REQ_CODE); }...@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK && requestCode == STORAGE_PERMISSION_REQ_CODE) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // Get the directory UrifolderUri = data.getData(); . }}}Copy the code

Once you get the Uri of the directory, you can walk through the files and do whatever you want with them. Once you’ve applied for permission, you don’t need to apply again. So we can persist the Uri of the directory (using SharedPreferences after converting the Uri to a String)

Next comes the code to traverse the file and modify the file format

// folderUri is the directory Uri you got
DocumentFile[] files = DocumentFile.fromTreeUri(this, folderUri).listFiles();
for (int i = 0; i < files.length; i++) {
    if (files[i].getName().endsWith(".myvideo")) {
        // Call the renameTo API of a DocumentFile to modify the file name
        files[i].renameTo(files[i].getName().replaceAll(".myvideo".".mp4")); }}//.mp4 to myvideo also reverses the code above
Copy the code

At this point, we have completed our requirements

About DocumentFiles and DocumentsContracts **

The document says that DocumentFile has a lot of overhead, so I suggest using DocumentsContract, but I really can’t find the relevant traversal operation. The API explains a little bit, but I can’t understand it very well. Therefore, the corresponding API of DocumentFile is used

Demand expansion

The layout file I wrote has only three buttons in it, so if you want to see changes in real time, you can introduce some FilePicker framework like FilePicker on github (I haven’t tried it yet).