Home > Java > javaTutorial > How to Write Files to a Specific Directory on an Android SD Card?

How to Write Files to a Specific Directory on an Android SD Card?

Linda Hamilton
Release: 2025-01-02 14:13:39
Original
957 people have browsed it

How to Write Files to a Specific Directory on an Android SD Card?

Writing to a Specific Folder on an SD Card in Android

When using the code snippet provided to download and write a file to the root directory of the SD card, you may want to specify a specific folder to write the file to. This can be achieved by modifiying the code to create a directory and then write the file to that directory.

File sdCard = Environment.getExternalStorageDirectory();  // Get the SD card root directory
File dir = new File (sdCard.getAbsolutePath() + "/dir1/dir2");  // Create a directory with the desired path
dir.mkdirs();  // Create the directory if it doesn't exist
File file = new File(dir, "filename");  // Create a new file in the directory

FileOutputStream f = new FileOutputStream(file);  // Open the file for writing
...
Copy after login

With this modification, the file will be written to the specified folder instead of the root directory of the SD card.

The above is the detailed content of How to Write Files to a Specific Directory on an Android SD Card?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template