Compartilhar via


Saving files programmatically (Android versus Windows Store apps)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Learn how to write code in Windows Store apps for Windows 8 to save files, like photos or documents. You can store these files in app-specific file locations or save them to external storage media, like a USB drive.

Introduction

A user may want to save a document that they're working on and immediately return to the saved document when they return to some document editor app. Or a user may want to save the current photo from some photo editing app to a USB drive and then connect that USB drive to a TV or a digital picture frame to view later. You can help your users do this by saving the document to an app-specific location on the user's device, and by saving the photo to external storage media.

Here's how app-specific file storage works behind the scenes in Windows Store apps. Each app can store files to a separate set of directories (called the local, roaming, and temporary directories) on a particular device:

  • The local directory contains files that exist only on the local device.
  • The roaming directory contains files that exist on all devices where the user has installed the app.
  • The temporary directory is like the local directory, but the system can remove its files at any time.

When a user installs a Windows Store app, Windows automatically creates these directories. You don't need to worry about where to find them because Windows tracks them for you. You just use the ApplicationData class to access them. If the user ever uninstalls the app, Windows automatically removes the matching directories so you don't even have to worry about cleaning them up.

Windows tip

If a user wants to store app-specific files that are particularly valuable or irreplaceable, have your app put them in a more permanent location instead, like the user's Microsoft OneDrive.

 

To store files in external storage media, you can use the DeviceInformation and StorageDevice classes. More about that later.

Here's how to do all of this in code.

Top

Save a file to an app-specific directory

In a Windows Store app app, to save a file named "hello.txt", write code like this for the local directory.

var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("Hello.txt");
await FileIO.WriteTextAsync(file, "Hello world!");

Or write code like this for the roaming directory.

var file = await ApplicationData.Current.RoamingFolder.CreateFileAsync("Hello.txt");
await FileIO.WriteTextAsync(file, "Hello world!");

And you can write code like this for the temporary directory.

var tempFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("TempFile.txt");
Android tip

In Android apps, write code like this for the local directory.

String filename = "hello.txt";
String string = "Hello world!";
FileOutputStream outputStream;
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();

Set the file-creation mode to MODE_PRIVATE so that other apps can't access your app's files. Windows Store apps, in contrast, can access only their own files.

Write code like this for the temporary directory.

File tempFile;
tempFile = File.createTempFile("TempFile.txt", null, context.getCacheDir());

 

Top

Save a file to external storage media

In a Windows Store app, to save a file to external storage media, first declare the Removable Storage capability in the app's Package.appxmanifest file on its Capabilities tab, like this.

After you declare this capability, check to see if external storage media is available using code like this.

public async Task<bool> IsExternalStorageAvailable()
{
    var devices = await DeviceInformation.FindAllAsync(StorageDevice.GetDeviceSelector());

    if (devices.Count > 0)
        return true;
    else
        return false;
}

If the external storage media is available, create the file using code like this.

var storage = StorageDevice.FromId(deviceInformation.Id);
await storage.CreateFileAsync("HelloWorld.png");
Android tip

In Android apps, to do something similar, first request the WRITE_EXTERNAL_STORAGE permission in your app's manifest, like this.

<manifest>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

After you request this permission, check to see if external storage media is available, using code like this.

public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

If the external storage media is available, you can then create the file. For example, to create a file in the media's Pictures directory, write code like this.

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "HelloWorld.png");

 

Top

Next steps

To learn more about saving files, see these resources:

Top