I want to update my xamarin app automatically by clicking update button from my settings page. for testing purpose, i've added the apk file in the assets folder. but the launcher didn't work. I've followed this solution but didn't work. Help plz?
This my update button click code
var cacheFile = Path.Combine(FileSystem.CacheDirectory, "App.apk");
using (var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.Assets.App.apk"))
using (var file = new FileStream(cacheFile, FileMode.Create, FileAccess.Write))
{
resource.CopyTo(file);
}
await Launcher.OpenAsync
(new OpenFileRequest()
{
File = new ReadOnlyFile(cacheFile)
}
);
AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
Related
I'm trying to display and embedded image in a shared project resource (as explained here on microsoft documentation).
I created the ImageResourceExtension, the project compiles, but I get the following error when I start the project :
Could not load type 'Xamarin.Forms.Xaml.IReferenceProvider' from
assembly 'Xamarin.Forms.Core, Version=2.0.0.0
Here's my code :
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App1"
x:Class="App1.MainPage">
<StackLayout>
<Image x:Name="imgTest" Source="{local:ImageResource img.jpg}" />
</StackLayout>
</ContentPage>
EmbeddedImage.cs
namespace App1
{
[ContentProperty(nameof(Source))]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Source == null)
return null;
// Do your translation lookup here, using whatever method you require
var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);
return imageSource;
}
}
}
You can try this, it works for me
xyz.xaml.cs
imgName.Source = ImageSource.FromResource("ProjectName.Images.backicon.png");
xyz.xaml
<Image x:Name="imgName" />
Hope this code helps you!!
Not knowing if the answer is still valid for you, but I usually have an Assets folder outside my solution which contains Android specific folders (drawables), iOS specific folders (Resource with #2x and #3x) and a Common folder for shared images.
In my solution I add these files as a link in the folders they should be associated with. On Android in the Resources/Drawable and iOS in the Resource. In this case Common files can be shared across both platforms without physically copying them.
As a bonus, updates to these Assets can be overwritten in the folder outside my solution and will be used within my solution without any additional action to be taken.
Replace:
<Image x:Name="imgTest" Source="{local:ImageResource img.jpg}" />
With:
<Image x:Name="imgTest" Source="{local:ImageResource 'Project.Folder.img.jpg'}" />
And Replace:
var imageSource = ImageSource.FromResource(Source, typeof(...));
With:
var imageSource = ImageSource.FromResource(Source);
If you add the Image to your Android drawable folder, or to the iOS Resource folder, you don't need any extension. You can simply do:
<Image x:Name="imgTest" Source="img.jpg" />
on Button click file picker window not open in xamarin forms-
I am using Xam.Plugin.FilePicker
Here is my button event code-
async void FilePickerEvent(object sender, EventArgs e)
{
try
{
FileData filedata = new FileData();
filedata = await CrossFilePicker.Current.PickFile();
byte[] data = filedata.DataArray;
string name = filedata.FileName;
foreach(byte b in filedata.DataArray)
{
string attachment = b.ToString();
}
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
I tried this plugin on Android device and it works well. I installed the plugin in all projects and then added these permissions to AndroidManifest.xml file as it is in the documentation.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Then in my MainPage.xaml I created a button
<Button Text="Open File" Clicked="Button_Clicked" />
And in my MainPage.xaml.cs created event
private async void Button_Clicked(object sender, EventArgs e)
{
var file = await CrossFilePicker.Current.PickFile();
}
I picked the file and retrieve the property FileName and DataArray without any problem.
There is a thread on xamarin forum or there is a issue list on their github.
There is a newer forked github project that received quite some fixes, e.g. for Android picking from download folder, OneDrive or Google Drive storage. The project is here:
https://github.com/jfversluis/FilePicker-Plugin-for-Xamarin-and-Windows
(note: I'm one of the contributors).
You just have to change the NuGet package name to Xamarin.Plugin.FilePicker, the API is the same.
I am creating a xamarin forms app and i want to change the transition when navigating between pages. I couldnt use https://components.xamarin.com/view/customnavpage because im using ContentPage. I also read the famous thread on xamarin forums but nothing worked for me. here is the link.
https://forums.xamarin.com/discussion/18818/custom-page-transitions-with-xamarin-forms
i'm using xamarin forms 2.3.4.247 and android api level 25 and appCompat 23.3.0.
any idea?
I found a workaround and I answer my own question maybe it helps someone else.
First we need to create a custom renderer for any platform(in this case android)
. create a new class under your Renderers folder in Droid target.
CustomTransitionsRenderer.cs
[assembly:ExportRenderer(typeof(NavigationPage),typeof(CustomTransitionsRenderer))]
namespace TesarSmartwatchForms.Droid.Renderers
{
public class CustomTransitionsRenderer : Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer
{
//SwitchContentAsync
protected override void SetupPageTransition(Android.Support.V4.App.FragmentTransaction transaction, bool isPush)
{
if (isPush)
transaction.SetCustomAnimations(Resource.Animator.move_in_left, 0);
else //prevView enter:
transaction.SetCustomAnimations(Resource.Animator.side_in_right, 0);
}
}
}
and then add your custom animations under Droid>Resources>animator like these:
move_in_left.xml
<?xml version="1.0" encoding="UTF-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="300"/>
</set>
and slide_in_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="300" />
</set>
then we need to modify our android theme just adding this line
<item name="android:windowBackground">#android:color/transparent</item>
and remember to navigate between pages with the default navigation transition, just like this
Navigation.PushAsync(page);
When the place picker launches, there is a 3-5 second long loading circle animation next to where the nearby places list should appear. After the loading circle disappears, no nearby places are shown in the list, and no nearby places pins are shown on the map.
Now, if pan the map (zoom doesn't do anything) in any direction a fair amount, the nearby places load just fine.
The first image below is taken after the place picker launches, it will stay this way without loading any nearby places until I pan the map.
The second image is taken after I have panned the map, where the nearby places loaded correctly.
At the bottom is my relevant code, let me know if you need more.
<manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<application
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="MY_API_KEY" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
// Emulator is using google play services version 9.4.52
dependencies {
compile 'com.google.android.gms:play-services-maps:9.4+'
compile 'com.google.android.gms:play-services-places:9.4+'
compile 'com.google.android.gms:play-services-location:9.4+'
}
#Override
protected void onCreate(Bundle savedInstanceState) {
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(this, this)
.build();
}
public void choosePlace() {
try {
PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder();
Intent intent = intentBuilder.build(this);
startActivityForResult(intent, PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
// This is not being thrown
} catch (GooglePlayServicesNotAvailableException e) {
// This is not being thrown
}
}
#Override
public void onConnectionFailed(ConnectionResult result) {
// This is not being called
}
Did you add this?
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="your_key" />
I had the same issue because I forgot to add meta-data to my manifest
I am using following code in an Android application written in Xamarin:
var recorder = new MediaRecorder ();
recorder.SetVideoSource (VideoSource.Camera);
recorder.SetAudioSource (AudioSource.Mic);
recorder.SetOutputFormat (OutputFormat.Default);
recorder.SetVideoEncoder (VideoEncoder.Default);
recorder.SetAudioEncoder (AudioEncoder.Default);
recorder.SetOutputFile (path);
recorder.SetPreviewDisplay (video.Holder.Surface);
recorder.Prepare ();
recorder.Start ();
It works but i have a problem. It uses background camera and orientation is landscape. How can I use front facing camera for recording the video and set orientation to portrait ?
Source: Android can't record video with Front Facing Camera, MediaRecorder start failed: -19
First, make sure that your permissions are set up correctly.
Specifically, to record video, you'll want:
<uses-feature android:name="android.hardware.camera.front" />
<uses-feature android:name="android.hardware.microphone"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> Second,
and this is the tricky part, this line from the tutorial does not work
with the front-facing camera!
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
That signature for CamcorderProfile.get() defaults to a profile for
the back-facing camera:
Returns the camcorder profile for the first back-facing camera on the
device at the given quality level. If the device has no back-facing
camera, this returns null.
Instead, use
http://developer.android.com/reference/android/media/CamcorderProfile.html#get(int,%20int).
The first parameter is the id of the camera that you opened,
specifically, the front-facing camera.
This is how you do it:
First make sure your minimum supported API version is Gingerbread 2.3
Second ensure all permissions have been added to manifest file
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
And use this snippet to get reference to front camera and display the preview in video view while recording
if (Camera.NumberOfCameras < 2) {
Toast.MakeText (this, "Front camera missing", ToastLength.Long).Show ();
return;
}
_camera = Camera.Open (1);
_camera.SetDisplayOrientation (90);
_camera.Unlock ();
recorder = new MediaRecorder ();
recorder.SetCamera (_camera);
recorder.SetAudioSource (AudioSource.Mic);
recorder.SetVideoSource (VideoSource.Camera);
recorder.SetOutputFormat (OutputFormat.Default);
recorder.SetAudioEncoder (AudioEncoder.Default);
recorder.SetVideoEncoder (VideoEncoder.Default);
recorder.SetOutputFile (path);
recorder.SetOrientationHint (270);
recorder.SetPreviewDisplay (previewVideoView.Holder.Surface);
recorder.Prepare ();
recorder.Start ();
I used #Kartik's solution with a small modification. Not all devices have a rear facing camera(2012 Nexus 7 is a great example). This verison of the code goes through all available cameras and checks if the camera is truly the front one.
for (int camIndex = 0; camIndex < Camera.NumberOfCameras; camIndex++)
{
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
Camera.GetCameraInfo(camIndex, cameraInfo);
if (cameraInfo.Facing == CameraFacing.Front)
{
try
{
_camera = Camera.Open(camIndex);
_camera.SetDisplayOrientation(270);
_camera.Unlock();
}
catch (Exception e)
{
Toast.MakeText(this, "Front camera missing", ToastLength.Long).Show();
}
}
}