I am using Xamarin for Android / iOS development. I have added a Shared Project within Xamarin solution. One of the functionality in the Shared project would be read assets within Android. However, AssetManager or Assets is not available from the Shared project. Any clue on how this can be used?
Maybe a bit late - hope it still helps somebody.
You can use
https://github.com/juergenkoller/PCLStorage
It has a GetFileFromAppBundleAsync function. You can place your file inside the asset folder of your Android project and do something like this:
var file = await FileSystem.Current.GetFileFromAppBundleAsync("your file.xyz", new CancellationToken(false));
var content = await file.ReadAllTextAsync ();
Related
I am trying to open Browser with a link inside my test method (application is launched prior to that). I installed Xamarin essentials Nuget, and per my current understanding I am not able to use this package outside xamarin project. I found some solutions (see below, using Mock object) but this does not seem to work.
var mockLaunch = new Mock();
IBrowser open = mockLaunch.Object;
open.OpenAsync("https://i389l.app.link/JFsYo6d1Y37", BrowserLaunchMode.SystemPreferred);
Does anyone have solution how to open browser (using real Android device) inside my test method? Thank you.
I have a Xamarin Android library project which is referenced by an Android application project.
The library project has layout files which need to be used by a library project component. The Resource.designer.cs file seems to be generated properly. The layouts are marked as Android Resources. Yet at runtime the resources are not there. Trying to access them just returns 0.
After reading all the SO questions on the topic, I am convinced this should work, but so far it doesn't. Ideas?
Create the Android Class Library.
Create a layout folder in this class library and create a layout in this folder.
Add the code in your xamarin android app.
var btn_Load = FindViewById<Button>(Resource.Id.btn_LoadLibraryLayout);
btn_Load.Click += delegate
{
SetContentView(ClassLibrary1.Resource.Layout.library_layout);
};
I create a button to open the layout in class library.
Result:
You could download from the ClassLibrary_layout folder of GitHub for reference.
https://github.com/WendyZang/Test.git
Updated:
If you want to get the resource identifier, you need the package name of this class library. First, we could not get the package name in AndroidManifest.xml file or properties, because the class library does not have it.
You could get it from the steps below.
Create a activity in class library. And get the package name with the code.
var PackageNname = Application.Context.PackageName;
You could also get the resource identifier in this activity.
var resId2 = Resources.GetIdentifier("library_layout", "layout", PackageNname);
Result:
The paskage name is same to the app1 I provided.
Package Name: com.companyname.app1
You could also use the code directly in app1 activity.
int resId2 = Resources.GetIdentifier("library_layout", "layout", "com.companyname.app1");
Creating folder by following code
var documents=Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
var directoryname = Path.Combine(documents, "XX");
Directory.CreateDirectory(directoryname);
but the folder does not exists in the specific path..May i know whats the reason.
regards..
if you need the main picture folder on the android then your code should look like this
string AndroidDcimFolder = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim).AbsolutePath;
From Xamarin Official Site:
Not all directories in this enumeration will be available on all
platforms for all users. When running on the Windows operating system,
the operating system determines the paths to these directories.
Enumeration means Environment.SpecialFolder.
You need to implement a DependencyService and need to call System.Environment.GetFolderPath from within the platform-specific implementation.
Check this sample.
As per my knowledge, it (your code) will not work in Xamarin.Form PCL project.
You can do another thing is, In your Android and iOS Project you can put this code to get the platform specific path:
using Xamarin Forms;
//Get current Path.
Config.PathApp = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
I am writing a Xamarin Forms PCL application (both iOS and Android)
I have to pick an image from phone gallery.
I have read some documentation about this plugin:
Labs.Platform.Services.Media.MediaPicker
In my Common project, i have to put this code:
var device = Resolver.Resolve<IDevice>();
picker = DependencyService.Get<IMediaPicker>() ?? device.MediaPicker;
But I have an error on the first line: IDevice and Resolver Objects are not know. I think I am missing a reference or a using clause.
Thanks
You need to install the following Nuget package in all of your projects in the solution (i.e. PCL core project, iOS project, and the Android Project, and UWP project if using it):
https://www.nuget.org/packages/XLabs.Forms/
And you will need to add the following using statements:
using XLabs.Ioc;
using XLabs.Platform.Device;
using XLabs.Platform.Services.Media;
I am working on Xamarin cross platform project where if I placed the images inside the sub folder it is not loading on the screen.
Please refer my code and folder structure along with properties
Put your Images folder under Assets Folder like this:
Assets/Images/*.png
and if you are using Localizing make like this:
Assets/Images/en/*.png
Assets/Images/another language/*.png
for more information you can check this Working with Images
i hope that answer you question.