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

How Can I Write Files to a Specific Folder on an Android SD Card?

Susan Sarandon
Release: 2024-12-29 21:14:19
Original
347 people have browsed it

How Can I Write Files to a Specific Folder on an Android SD Card?

Writing to a Specific Folder on an SD Card in Android

When accessing external storage on Android, it's often desirable to write files to specific folders rather than the root directory. This ensures organized storage and allows apps to isolate their data.

Customizing the Download Path

The provided Downloader class downloads files to the root directory. To specify a custom folder, follow these steps:

  1. Create a File object representing the desired directory. For instance, to create the /mnt/sdcard/myapp/downloads folder:
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/dir1/dir2");
dir.mkdirs();
Copy after login
  1. Replace the root variable in the FileOutputStream line with the new dir variable:
FileOutputStream f = new FileOutputStream(new File(dir, fileName));
Copy after login

With these changes, the downloaded file will now be written to the specified folder.

The above is the detailed content of How Can I Write Files to a Specific Folder 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