Over a million developers have joined DZone.

Android to Windows 8: Working with Files

DZone's Guide to

Android to Windows 8: Working with Files

· Cloud Zone ·
Free Resource

Learn how to migrate and modernize stateless applications and run them in a Kubernetes cluster.

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.

A user may want to save a document that they’re working on and immediately return to the saved document it 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 localroaming, 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 theApplicationData 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 store them in a more permanent location instead, like the user’s SkyDrive.

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

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

Save a file to an app-specific directory

In a Windows Store 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.
File tempFile;
tempFile = File.createTempFile("TempFile.txt", null, context.getCacheDir());

String filename = "hello.txt";
String string = "Hello world!";
FileOutputStream outputStream;

outputStream = openFileOutput(filename, Context.MODE_PRIVATE);

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.

Saving a File to External Storage

In a Windows Store app you must declare the Removable Storagecapability in the in the Package.appsxmanifest associated with your app

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;
    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 theWRITE_EXTERNAL_STORAGE permission in your app’s manifest, like this.

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

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");

Join us in exploring application and infrastructure changes required for running scalable, observable, and portable apps on Kubernetes.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}