Created
July 31, 2012 13:32
-
-
Save erikkerber/3217066 to your computer and use it in GitHub Desktop.
MonoDroid Resource to FileSystem management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Android.Content.Res; | |
using System.Resources; | |
using System.IO; | |
using Android.Content; | |
using Android.App; | |
namespace AndroidSandbox | |
{ | |
public static class ResourceHelper | |
{ | |
private static Dictionary<string, string> _Hash = new Dictionary<string, string>(); | |
/// <summary> | |
/// Takes in a resource name, and returns the SD card path that the file resides on. | |
/// | |
/// If the file does not exist on the SD card, it will copy it over and add it to the | |
/// hash of known file locations. | |
/// </summary> | |
/// <returns> | |
/// The path for resource. | |
/// </returns> | |
public static string GetPathForResource (this Activity activity, string resource) | |
{ | |
if (_Hash.ContainsKey (resource)) | |
return _Hash [resource]; | |
else { | |
var outDir = Path.Combine (Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, resource); | |
if(File.Exists (outDir)){ | |
_Hash.Add (resource, outDir); | |
return outDir; | |
} | |
int resID = activity.Resources.GetIdentifier(resource , "raw", activity.PackageName); | |
using (var input = activity.Resources.OpenRawResource(resID)) | |
using (StreamReader sr = new System.IO.StreamReader(input)) | |
using (StreamWriter sw = new StreamWriter(outDir)) | |
{ | |
sw.Write (sr.ReadToEnd()); | |
} | |
_Hash.Add(resource, outDir); | |
return outDir; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment