Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify file format / file name in batch by Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how Android modifies the file format / file name in batches. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Preface

Sometimes we may encounter the need to modify the file format in batches, but Baidu has not found any related applications for a long time, so after studying Android for two days, I want to get an APP to achieve this requirement.

1. Use shell scripts to modify file formats / file names in batches

The first method was my first success, because android itself was modified by Linux, so it would make sense to use a little bit of shell command. And this approach should ignore the fact that any version of Android (such as Android 1-11) can be successful, which is briefly described below.

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

# iterate through all .mp4 files for name in * .mp4do# to modify the file format, and randomly choose the name mv "$name"${name%.mp4} .myvideo" done

Then change the suffix name of the text file where the code is stored to .sh, and then find any application that can execute .sh (script file) or open the terminal (if you open your own terminal, remember chmod 777 script.sh to modify file permissions), you can execute the code. I use the MT manager to execute it (Kuan can be the next one, Kuan-> MT manager).

Tell me about the advantages and disadvantages of this method.

The advantage is that it does not involve much code knowledge, and the shell script only has a for loop, and the code can be easily modified into code that meets the corresponding requirements, which can be said to be zero burden to solve the requirements, and different versions of Android can be modified in batches as long as they can execute script files, with strong compatibility.

Disadvantages, disadvantages are obvious, you need to download the application (you don't know if the application is risky, there may be advertisements, and no one will write an APP specifically for batch files, unless you develop it personally)

two。 Use DocumentFile to modify file formats / file names in batches

This method is in line with the theme of the article, write your own APP to achieve this requirement.

As I said earlier, script files are my first successful solution. Before that, I always looked at using java to implement batch files, which was possible according to most tutorials on the Internet before Android 10 (API 29). But Android 10 enables partition storage, and each application can only access its own private space and a few external shared storage spaces specified by Android (such as media content in photo albums). Files under the Download folder)

At this time, using the conventional way of File#renameTo () will not work on the android 10 machine, you need to obtain the appropriate permissions to operate on the file (files generated by other applications, files you apply yourself can still be modified at will).

At first, I just wanted to change the format of the video file. According to the update (link) of the media file introduced in the android official document, we need to obtain the user's consent to modify the file by throwing the RecoverableSecurityException on the capture platform, which runs counter to the title of our article. If I want to apply for permission one by one, I might as well use the local file manager to rename the files one by one, or I can change the file format / file name.

Abandoning the official method of updating media files and using another scheme given by official documents to access documents and other files from shared storage space, users only need to grant access to the directory where the batch files are located once. The files under the directory can be modified arbitrarily, and the corresponding logic can be written with the idea. The code is as follows

. / * * use ACTION_OPEN_DOCUMENT_TREE intent operation * to support users to select a specific directory and grant the application access to all files * and subdirectories in that directory * / public void getPermission () {Intent intent = new Intent (Intent.ACTION_OPEN_DOCUMENT_TREE); intent.addFlags (Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivityForResult (intent, STORAGE_PERMISSION_REQ_CODE) }. @ Overrideprotected 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 Uri folderUri = data.getData ();.}

After getting the Uri of the directory through the above code, you can traverse the files in it and do whatever you want with them. After applying for permission once, there is no need to apply again, so we can persist the Uri of the directory (convert Uri to String and use SharedPreferences).

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

/ / folderUri is the directory you obtained UriDocumentFile [] files = DocumentFile.fromTreeUri (this, folderUri). ListFiles (); for (int I = 0; I)

< files.length; i++) { if (files[i].getName().endsWith(".myvideo")) { // 调用 DocumentFile 的 renameTo API就可以修改文件名了 files[i].renameTo(files[i].getName().replaceAll(".myvideo", ".mp4")); }}// .mp4 to myvideo 也是上面的代码反转一下 至此我们就完成了相应的需求啦 关于 DocumentFile 和 DocumentsContract** 文档里面说 DocumentFile 有大量开销,建议用 DocumentsContract,但我实在是找不到相关的遍历操作,API 里面讲了一点,但我这种 Android 小白不是很能看得懂啊,所以还是用了 DocumentFile 相应的 API 需求扩展 我写的布局文件里面只有三个按钮,如果需要实时观察变化的话,可以引入一些文件选择器框架进行查看,比如 github 里面的 FilePicker(我没有试过)

On "Android how to batch modify file format / file name" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report