Welcome!

@DXWorldExpo Authors: Elizabeth White, Zakia Bouachraoui, Liz McMillan, Pat Romanski, Carmen Gonzalez

Blog Feed Post

Android to Windows 8: Working with Files

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 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 store them in a more permanent location instead, like the user’s SkyDrive.

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.

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.

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

Saving a File to External Storage

In a Windows Store app you must declare the Removable Storage capability 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;
   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");

Next steps

To learn more about saving files, see these resources.

Read the original blog entry...

More Stories By Adam Grocholski

Hey there! My name is Adam Grocholski, and I'm a Technical Evangelist at Microsoft where I spend time focusing on Windows, Windows Phone, and Windows Azure. I live in the frozen tundra of Minnesota and run thinkfirstcodelater.com. You can also follow me on twitter at @codel8r.

DXWorldEXPO Digital Transformation Stories
The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected pat...
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
In his general session at 21st Cloud Expo, Greg Dumas, Calligo’s Vice President and G.M. of US operations, discussed the new Global Data Protection Regulation and how Calligo can help business stay compliant in digitally globalized world. Greg Dumas is Calligo's Vice President and G.M. of US operations. Calligo is an established service provider that provides an innovative platform for trusted cloud solutions. Calligo’s customers are typically most concerned about GDPR compliance, application p...
Druva is the global leader in Cloud Data Protection and Management, delivering the industry's first data management-as-a-service solution that aggregates data from endpoints, servers and cloud applications and leverages the public cloud to offer a single pane of glass to enable data protection, governance and intelligence-dramatically increasing the availability and visibility of business critical information, while reducing the risk, cost and complexity of managing and protecting it. Druva's...
BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.
With 10 simultaneous tracks, keynotes, general sessions and targeted breakout classes, @CloudEXPO and DXWorldEXPO are two of the most important technology events of the year. Since its launch over eight years ago, @CloudEXPO and DXWorldEXPO have presented a rock star faculty as well as showcased hundreds of sponsors and exhibitors! In this blog post, we provide 7 tips on how, as part of our world-class faculty, you can deliver one of the most popular sessions at our events. But before reading...
DSR is a supplier of project management, consultancy services and IT solutions that increase effectiveness of a company's operations in the production sector. The company combines in-depth knowledge of international companies with expert knowledge utilising IT tools that support manufacturing and distribution processes. DSR ensures optimization and integration of internal processes which is necessary for companies to grow rapidly. The rapid growth is possible thanks, to specialized services an...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...