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
Susan SarandonOriginal
2024-12-29 21:14:19381browse

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();
  1. Replace the root variable in the FileOutputStream line with the new dir variable:
FileOutputStream f = new FileOutputStream(new File(dir, fileName));

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!

Statement:
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