How to integrate a file picker with WASM? - uno-platform

I want to upload an image from my PC and show it in a grid view.
How can I choose images from my PC and list them out in WASM?

Official support for this was added in Uno 3.6. You can use the FilePicker APIs to pick files and folders on any platform.
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".jpg");
var file = await picker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
this.textBlock.Text = "Picked photo: " + file.Name;
}
else
{
this.textBlock.Text = "Operation cancelled.";
}

Related

What is the path of the Json file in Android at Xamarin.Forms?

I am developing an application for Android using Xamarin.
I have created a JsonData folder in the Android project and created a Setting.json file.
\MyApp\MyApp.Android\JsonData\Setting.json
In the properties, we set the Copy when new.
The following folders in the local environment contain the files.
\MyApp\MyApp.Android\bin\Debug\JsonData\Setting.json
I want to load this file in the actual Android device.
When I do this, it tells me that the file is missing.
Could not find a part of the path "/JsonData/Setting.json."
Try
{
var text = File.ReadAllText("JsonData/Setting.json", Encoding.UTF8);
var setting = JsonConvert.DeserializeObject<Setting>(text);
}
catch(Exception exception)
{
var error = exception.Message;
}
What is the path of the file in Android?
I think you're using File Handling in Xamarin.Forms incorrectly.
From the parameter of function File.ReadAllText, the app will access the file system to getSetting.json from folder JsonData in your android device.
The path of the file on each platform can be determined from a .NET Standard library by using a value of the Environment.SpecialFolder enumeration as the first argument to the Environment.GetFolderPath method. This can then be combined with a filename with the Path.Combine method:
string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "temp.txt");
And you can read the file by code:
string text = File.ReadAllText(fileName);
In addition, from your code,I guess you want to Load your Embedded file( Setting.json) as Resources,right?
In this case,we should make sure the Build Action of your Setting.json is Embedded Resource.
And GetManifestResourceStream is used to access the embedded file using its Resource ID.
You can refer to the following code:
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(LoadResourceText)).Assembly;
Stream stream = assembly.GetManifestResourceStream("YourAppName.JsonData.Setting.json");
string text = "";
using (var reader = new System.IO.StreamReader (stream))
{
text = reader.ReadToEnd ();
}
For more , you can check document : File Handling in Xamarin.Forms.
And you can also check the sample code here: https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/workingwithfiles/ .

Working way to transfer SQLite database to another device without external storage permissions (updates Android 11)

With new Shared Storage and new Android API 30 requirements I can't find a way to recover a database on new or another device.
Until this time, I used File API to create a copy of the database. Any time user could put this file to any cloud, download from cloud on another device and restore the database on it. Of course, I used WRITE_EXTERNAL_STORAGE.
Now, Google rejects the app and says that we should use MediaStore API which can solve that issues without any permissions. I tried it and it works only on same device. And until if created the database file not copied to/from cloud or moved to/from another folder. In that case cursor.getCount == 0:
String[] projections = new String[]{MediaStore.Downloads._ID};
String selection = MediaStore.Downloads.DISPLAY_NAME + "=?";
String[] selectionArgs = new String[]{"database.db"};
String sortOrder = MediaStore.Downloads.DISPLAY_NAME + " ASC";
Cursor cursor = getApplicationContext().getContentResolver().query(MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL), projections, selection, selectionArgs, sortOrder);
Uri uri = null;
if (cursor != null) {
int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Downloads._ID);
cursor.moveToNext();
long id = cursor.getLong(idColumn);
uri = ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI, id);
try {
ContentResolver contentResolver = getApplicationContext().getContentResolver();
ParcelFileDescriptor parcelFileDescriptor = contentResolver.openFileDescriptor(uri, "rw");
FileInputStream fileInputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
File dbFile = new File(Environment.getDataDirectory(), "/data/com.example.app/databases/database.db");
FileOutputStream fileOutputStream = new FileOutputStream(dbFile);
byte[] buf = new byte[1024];
int len;
while ((len = fileInputStream.read(buf)) > 0) {
fileOutputStream.write(buf, 0, len);
}
fileOutputStream.close();
fileInputStream.close();
parcelFileDescriptor.close();
restartApp();
} catch (IOException e) {
e.printStackTrace();
}
cursor.close();
}
Something wrong with code or MediaStore API does not provide any actions with created files?
E/DBError: exception
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
Also I tried Google Drive API to copy the database file on cloud directly from app and download it. But it's not working way too, because new device don't know about file ID which used to download files.
Any thoughts on this?
Figured out.
Without any storage permissions your app have access to onw created files with MediaStore API.
But haven't if:
file was moved/copied inside device storage
file was uploaded to cloud and then downloaded from it
app was reinstalled
I tested it on versions 28 (emulation), 29 (device), 30 (emulation).
So in my case anyway I need:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="29"/>
Don't know why but for read own file on Android Q not enough only READ_EXTERNAL_STORAGE
I was lucky and Google approved app. Hope you too.

Xamarin - CachedImage - Access the downloaded file

I am using the CachedImage component of ffimageloading. I have a kind of gallery with a carousel view.
All the images are loaded through an internet URL, they are not local images. I would like to add the image sharing function. But I don't want to download the file again, I would like to know if there is a way to access the file that the CachedImage component already downloaded to be able to reuse it in the share function.
try using MD5Helper
var path = ImageService.Instance.Config.MD5Helper.MD5("https://yourfileUrlOrKey")'
Thanks Jason
I share with you how part of my code is:
var key = ImageService.Instance.Config.MD5Helper.MD5("https://yourfileUrlOrKey");
var imagePath = await ImageService.Instance.Config.DiskCache.GetFilePathAsync(key);
var tempFile = Path.Combine(Path.GetTempPath(), "test.jpg");
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
File.Copy(imagePath, tempFile);
await Share.RequestAsync(new ShareFileRequest
{
Title = "Test",
File = new ShareFile(tempFile)
});
The temporary file I believe, since the cached file has no extension and the applications do not recognize the type.

FailedToExecuteCommand ghostscript gswin64c.exe on azure

I am trying to convert pdf into images using magickNET and ghostscript. It works well on local and windows VMS servers. But when i push to azure servers it gives me the error saying FailedToExecuteCommand ghostscript gswin64c.exe. Details of error in attachements.
I tried changing the path for ghostscript dll but found no luck. The path is correct and dll file is there. One thing I am sure of. I have put dll inside wwwroot/GhostScriptDll. on error it shows "D:/home/site/wwwroot/wwwroot/GhostScriptDll/gswin64c.exe" which is also correct there are 2's wwwroot.
can someone help me to use ghostscript without webjobs things.
C# code i tried is like this
enter image description here
string rawpath = _hostingEnvironment.WebRootPath + "\\GhostScriptDll"
MagickNET.SetGhostscriptDirectory(rawpath);
MagickReadSettings settings = new MagickReadSettings();
settings.Density = new Density(300, 300);
List<string> intList = new List<string>() { };
using (MagickImageCollection images = new MagickImageCollection())
{
images.Read($"{rawFileLocation}{model.OriginalDocName}", settings);
int page = 1;
string tempName = "";
foreach (MagickImage image in images)
{
tempName = System.IO.Path.GetRandomFileName().Replace(".", string.Empty) + "_processed_" + page + ".png";
image.Write($"{processedFileLocation}{tempName}");
intList.Add(tempName);
page++;
}
}
i doubt in this part of error D:/local/Temp/magick-MB0cs71xMgZUfIHIwbzlzehBIlLL-NED"' (The system cannot find the file specified.
) # error/delegate.c/ExternalDelegateCommand/475 ...i dont find the local/temp/.. folder in server though

provider.FileData null in ReadAsMultipartAsync with media files

I am uploading files by using HTML File Upload control and passing data to ASP.NET Web API. Everything is working fine when I upload image or any text file or xml file. but it's giving error while I upload media files (mp3, mp4).
My code is,
string path = "//My path";
MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(path);
var task = Request.Content.ReadAsMultipartAsync(provider);
return await task.ContinueWith<bool>(t =>
{
string originalSavedAt = "";
foreach (MultipartFileData file in provider.FileData)
{
originalSavedAt = file.LocalFileName;
}
return true;
}, TaskScheduler.FromCurrentSynchronizationContext());
In above code, When I upload image file, it show me values in provider. FileData (provider.FileData.count is 1) in foreach loop.
But it is null (provider.FileData.count is 0) when I upload media file.
Status is failure in task lembda expression during media file upload, but status is RanToCompletion during image upload.
Do I need to add anything for media files. ?

Resources