How to place file in Android's external storage when installing application. #7011
-
What would you like to be added:I can't figure out how to place asset files in private external storage on Android.
But the open method says the file is not found. It would be nice if there was an example of how to deploy the files in external storage when application is installed or at least how can I access any asset file so I could copy it manually. Why is this needed:I want the user to be able to place he's own sounds packages easily inside the common directory so it can be chosen instead of default one. For example user will create directories structure in For which Platform:
Anything else we need to know? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
You need to add the file to project as an embedded resource. BuildAction -> EmbeddedResource. To use the data stream in a cross-platform way you can do something like: Assembly assembly = Assembly.GetExecutingAssembly();
string resourcePath = assembly.GetManifestResourceNames().FirstOrDefault(
resourceName => resourceName.EndsWith("fileName" + "extension", StringComparison.OrdinalIgnoreCase));
using (Stream stream = assembly.GetManifestResourceStream(resourcePath))
{
using (StreamReader reader = new StreamReader(stream))
{
var text = reader.ReadToEnd();
}
} Copying the stream to an external location can be done using other tutorials for either mono/.NET or WinRT. You also need to make sure permissions are setup. |
Beta Was this translation helpful? Give feedback.
-
Ok, if that's the only way I will try it. I've already spend half of yesterday trying to figure out Android's file system and I think the copy thing should not be an issue, but setting file as embedder resource will be problematic. Apart form wasting storage space, the program was already working on windows but when I set the file to be embedded resource it would no longer appear in programs directory and root directories will be completely different... I still don't understand where the file goes on android when i set it as "Content" in a shared project. The other .png file I use in the project is set to content and is reused for all platforms. Are "Content" assets limited to picture files only? |
Beta Was this translation helpful? Give feedback.
-
Oh, I see. I believe Uno should automatically transform Content into an Android Asset. I don't think Embeded Resource is the only way, but it is what I've used in the past. I don't need the file to appear in the file system with the executable though. https://stackoverflow.com/a/64483046
|
Beta Was this translation helpful? Give feedback.
-
Check for this document about |
Beta Was this translation helpful? Give feedback.
-
Thank you guys, I will post how I did it when I succeed. |
Beta Was this translation helpful? Give feedback.
Check for this document about
StorageFile.GetFileFromApplicationUriAsync()
, you can also take a look at this sample.