I'm trying to make an android app with a webview which can upload images to a html page.
I think i found a solution here: Android ACTION_IMAGE_CAPTURE Intent
but i'm having trouble translating everything to Xamarin c#, any can help me here? the code i'm interestet in is:
File imageDirectory = new File("/sdcard/signifio");
String path = imageDirectory.toString().toLowerCase();
String name = imageDirectory.getName().toLowerCase();
ContentValues values = new ContentValues();
values.put(Media.TITLE, "Image");
values.put(Images.Media.BUCKET_ID, path.hashCode());
values.put(Images.Media.BUCKET_DISPLAY_NAME,name);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Media.DESCRIPTION, "Image capture by camera");
values.put("_data", "/sdcard/signifio/1111.jpg");
uri = getContentResolver().insert( Media.EXTERNAL_CONTENT_URI , values);
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
i.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(i, 0);
more specific it's what classes i have to import to find Media.TITLE, Images.Media.BUCKET_ID.... and so forth.
Try Android.Provider.MediaStore
If they aren't in their you could use the actual strings instead, see the Android documentation
Here's the call to getContentResolver Xamarin style:
this.ContentResolver.Insert(Android.Provider.MediaStore.Images.Media.ExternalContentUri, values);
For the location of the file use:
Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures), "CameraAppDemo");
There is a recipe for using a camera intent in the Xamarin documentation.
Related
The code I have now is:
Intent intent = new Intent();
intent.SetType("text/plain");
intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(intent, "Select file"), 1);
string str = System.Environment.GetFolderPath(?????);
That last line of course doesn't work. I'd like the user to be able to browse folders on the device and open one of them. This is simple in Windows but Android is proving difficult.
1/3/2023:
This issue is still unsolved. Coming from Windows and C# this is all trivial. But Android, not.
I have tried these:
var documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
var documents = Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory);
var documents = System.Environment.CurrentDirectorygetExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
WIth some I can save the file from my app on Android but I cannot find them (non-programmatically).
When I save files in FXTextEdit (on Android), I save them to folders I can easily access. Why not progammatically?
I am coding in C#.
Thanks.
I have Created a AppResource.Resx and and AppResource.da.resx and i have also Resources string in String.XML with Value-da => String.XML
AppResources.Culture = new CultureInfo("da-DK");
Java.Util.Locale.Default = new Java.Util.Locale("da", "DK");
Resources.Configuration.Locale = Java.Util.Locale.Default;
Resources.UpdateConfiguration(Resources.Configuration, Resources.DisplayMetrics);
var sfs = Resources.GetText(Resource.String.AboutLabel);
var dfdd = AppResources.AboutLabel;
Finish();
Intent intent = new Intent(this, this.Class);
StartActivity(intent);
var dffdsadd = AppResources.AboutLabel;
var fdsfdsafs = Resources.GetText(Resource.String.AboutLabel);
here is I'm Trying to set or update the Resources of android and these resources get from AppResource.resx file but not updating resources?
How i can bind AppResources string with android String so localization and value access?
When you import files, translate files, export files, for use in your Xamarin mobile apps, you could use one of the following file formats:
RESX files for Xamarin.Forms
Localizable.strings files for the Xamarin.iOS native platform
Android Strings files for the Xamarin.Android native platform
Do not bind AppResources string with android String so localization and value access. You could download the sample code from the link for reference.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/localization/text?pivots=windows
sorry im very new to unity and c#.
Im trying to read an image and apply it as a texture.
The code works when i try it with an image on my computer, however, when i try it with my android devices (phone and AR glasses), i've not been able to specify the file path correctly. How do i specify the file path properly of android devices or is there a way i can get these file path?
Thank you so much for any help in advance! :)
void Start()
{
thisTexture = new Texture2D(100, 100);
//string path = "C:/Users/kenny/Desktop/5th March/im.png"; //this works
string path = "file:///storage/emulated/0/im2.png"; // this doesnt work
bytes = File.ReadAllBytes(path);
thisTexture.LoadImage(bytes);
GetComponent<Renderer>().material.mainTexture = thisTexture;
}
For mobile devices it is not as straightforward to get a file from a plain path.
One way of doing so is by using the Resources folder. In your root folder (Assets), create a new folder named Resources. Put your image there.
Then you can do something like this:
// path without file extension!
var texture = Resources.Load<Texture2D>("path/to/texture");
You can get more references for this functionality here: https://forum.unity.com/threads/how-to-load-a-image-from-the-resources-folder-to-a-texture2d.101542/
Alternatively, you could use the StreamingAssets folder, but that's another story.
Basically I'm trying to make an UWP app to create Custom Tiles on Start Menu.
The SecondaryTile is working when the Tile Images are from Assets folder like this.
Uri square150x150Logo = new Uri("ms-appx:///Assets/square150x150Tile-sdk.png");
Uri wide310x150Logo = new Uri("ms-appx:///Assets/wide310x150Tile-sdk.png");
Uri square310x310Logo = new Uri("ms-appx:///Assets/square310x310Tile-sdk.png");
Uri square30x30Logo = new Uri("ms-appx:///Assets/square30x30Tile-sdk.png");
But I have images in ApplicationData folders and trying to use the path like this:
Uri square150x150Logo = new Uri(Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "ImageSquare.png"));
Uri wide310x150Logo = new Uri(Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "ImageWide.png"));
Uri square310x310Logo = new Uri(Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "ImageLarge.png"));
Uri square30x30Logo = new Uri(Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "ImageTiny.png"));
But this way the app is crashing. The debugger isn't working either. Any solutions?
To reference application data files, you should be using ms-appdata:///.
Calling TemporaryFolder.Path will return the absolute file path, C:\, which isn't allowed to be used on secondary tiles.
So your updated code would be...
Uri square150x150Logo = new Uri("ms-appdata:///temp/ImageSquare.png"));
OK finally figured it out.
In Addition to Andrew's answer, I had to change to using the LocalState folder instead of the TempState.
Andrew directed in the right way to use ms-appdata, but for some reason ms-appdata:///temp/ returned error. but using ms-appdata:///Local/ solved the Issue.
I'm using Xamarin iOS. I'd like to have our application open a .pdf file in the default reader. For Android I managed to get it done by following a two-fold process: (1) copy the file from the Assets folder to the Android device's Downloads folder, and (2) starting an activity to view the file.
I cannot find any similar resources for the iOS. We don't want to use the control in the Xamarin website, as it involves downloading lots of JavaScript (?) code, making the size of the app bigger, and also doesn't support pinching the screen to zoom. We want to use the OS's default pdf reader.
Thank you.
Method 1:
Set the Build Action to BundleResource. You can set the build action for a file by right-clicking on that file and and choosing Build Action in the menu that opens.
Create a UIWebView and add it to a view:
webView = new UIWebView (View.Bounds);
View.AddSubview(webView);
Load the file using NSUrl and NSUrlRequest classes:
string fileName = "Loading a Web Page.pdf"; // remember case-sensitive
string localDocUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
webView.LoadRequest(new NSUrlRequest(new NSUrl(localDocUrl, false)));
webView.ScalesPageToFit = true;
Method 2:
public void OpenFile (NSUrl fileUrl)
{
var docControl = UIDocumentInteractionController.FromUrl (fileUrl);
var window = UIApplication.SharedApplication.KeyWindow;
var subViews = window.Subviews;
var lastView = subViews.Last ();
var frame = lastView.Frame;
docControl.PresentOpenInMenu (frame, lastView, true);
}