I want to convert Image to stream from the ImageSource.FromFile.
My Code:
string location = Path.Combine(FileSystem.AppDataDirectory, "data", "Demo.jpg");
image.Source = ImageSource.FromFile(location);
The image is shown properly but I want to convert this image to Stream in Forms.UWP.
I tried the await StorageFile.GetFileFromApplicationUriAsync() this code to get the stream of the image from the location but it throws an exception.
Please suggest me how to convert the image from the file to stream in UWP?
Santhiya A
This path Path.Combine(FileSystem.AppDataDirectory, "data", "Demo.jpg"); is app's local path within UWP platform. And it is not app installation path. if place the Demo.jpg in your project folder, you will not find it with above path. And the parameter of ImageSource.FromFile(string file) is file name, but not the complete path. For the detail please refer this case reply.
I tried the await StorageFile.GetFileFromApplicationUriAsync() this code to get the stream of the image from the location but it throws an exception.
For UWP platform, you could use the following to converter the image file(build property is Content) to steam. For more please refer this document.
private async Task<Stream> GetStreamAsync()
{
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Demo.jpg"));
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
return stream.AsStream();
}
And if you want use above in Xamarin Forms client project, you need package above method with DependencyService.
Related
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/ .
I am creating json files FileSystem.AppDataDirectory, "test"," {Hour}.json "; I can see the files on the device in Xcode.. once I return only one with this Path.Combine(FileSystem.AppDataDirectory, "test",".json") then I can read its content.
However once I need to return all files from the directory and list the path to them in my application
var result = Directory.EnumerateFiles(FileSystem.AppDataDirectory, "test");
The result is empty
this is the path /var/mobile/Containers/Data/Application/02A91048-0016-4E20-A8A6-EB2A89649F1F/Library
which is correct an I see the files the physical. Where am I doing mistake?
I have also tried this
var test = FileSystem.OpenAppPackageFileAsync(FileSystem.AppDataDirectory);
but I am getting unauthorized exception
you are looking for files in the test subfoloder
the signature of EnumerateFiles is
public static System.Collections.Generic.IEnumerable<string> EnumerateFiles (string path, string searchPattern);
so you want to do this
var path = Path.Combine(FileSystem.AppDataDirectory,"test");
var result = Directory.EnumerateFiles(path, "*.json");
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.
I try to eneable audio in my Xamarin.Forms application. I want to have the audio files in a Subfolderof the Assetsfolder like this Assets/Subfolder/Audio.mp3
I have found a plugin SimpleAudioPlayer which provide an example.
The following code works like provided.
var player = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
player.Load("Audio.mp3");
Now I want to place the audio file in a subfolder and I tried to call
player.Load("Subfolder/Audio.mp3");
But I get an Java.IO.FileNotFoundException
I looked then into the implementation of the Load function and I fould the following code
public bool Load(string fileName)
{
player.Reset();
AssetFileDescriptor afd = Android.App.Application.Context.Assets.OpenFd(fileName);
player?.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
return PreparePlayer();
}
where the filename is pasted to the Assets.OpenFd() function. which returns an AndroidFileDesriptor
The documentation does not really provide any information from Microsoft and from the Android site.
My questions are
How can I receive the file from the subfolder in Android Assets?
What can I paste into the Assets.OpenFd() function (subfolders etc)?
I would appreciate any advice, since after a long time trying to resolve it I don't really have an idea.
According to the docs of the package, you won't be able to do just that because you have to use player.Load(GetStreamFromFile("mysound.wav")); where GetStreamFromFile is basically
Stream GetStreamFromFile(string filename)
{
var assembly = typeof(App).GetTypeInfo().Assembly;
var stream = assembly.GetManifestResourceStream("App." + filename);
// Obviously App in the line above should be replaced with your app
return stream;
}
And secondly you have to player.Load(GetStreamFromFile("Subfolder.mysound.wav")); where Subfolder is the name of your subfolder.
In an app, I am taking a screenshot of the content of a page, and I want to save it in the Gallery, create a local notification with the image in it, and when the user taps on it, it open the image from the Gallery.
As of now, I am able to take the snapshot and save it using this :
var path = MediaStore.Images.Media.InsertImage(context.ContentResolver, bitmap, "TestImage.png", "Snapshot taken");
I have a few problems with this approach, I can see the image in the Pictures folders from the Gallery, but the name is not the one I specified in this case "TestImage.png". Also, the path returned from this call has the URI scheme "content://...".
After that, I create the local notification with NotificationCompat.Builder with the embedded image, it works well. But when I try to add the PendingIntent to it to open the image I have an issue with the path returned from InsertImage call. The path returned is :
content://media/external/images/media/1292
I tried this :
var path = FileProvider.GetUriForFile(CrossCurrentActivity.Current.AppContext, "com.myapp.fileprovider", new File(path));
but it doesn't work, I get an exception :
Java.Lang.IllegalArgumentException: Failed to find configured root that contains /content:/media/external/images/media/1292
I also tried this :
global::Android.Net.Uri.FromFile(new File(path)), "image/*")
but I receive another exception :
Android.OS.FileUriExposedException: file:///content%3A/media/external/images/media/1292 exposed beyond app through Intent.getData()
This is the code I use to create the notification:
NotificationCompat.BigPictureStyle picStyle = new NotificationCompat.BigPictureStyle();
picStyle.BigPicture(bitmap);
picStyle.SetSummaryText("The snapshot has been saved to your library");
const int pendingIntentId = 0;
PendingIntent pendingIntent =
PendingIntent.GetActivity(CrossCurrentActivity.Current.Activity, pendingIntentId,
new Intent().SetAction(Intent.ActionView)
.SetDataAndType(global::Android.Net.Uri.FromFile(new File(path)), "image/*"), PendingIntentFlags.OneShot);
NotificationCompat.Builder builder = new NotificationCompat.Builder(CrossCurrentActivity.Current.AppContext, "UniqueID")
.SetContentIntent(pendingIntent)
.SetContentTitle("Security center")
.SetContentText("The snapshot has been saved to your library")
.SetSmallIcon(Resource.Drawable.ic_notification);
builder.SetStyle(picStyle);
Notification notification = builder.Build();
NotificationManager notificationManager =
CrossCurrentActivity.Current.Activity.GetSystemService(Context.NotificationService) as NotificationManager;
const int notificationId = 0;
notificationManager.Notify(notificationId, notification);
Do you guys have any idea on how I could save the image with the right name in the Public Gallery folder then access it in the PendingIntent for the local notification ?
Thank you all !
Okay so after a few hours of trial and error, I found out the issue, when parsing the path returned from the MediaStore.Images.Media.InsertImage, I had to use global::Android.Net.Uri.Parse() and not Uri.FromFile(), the latter is only if your path has the URI scheme "file://". By doing that I was able to make the ContentIntent work on the local notification. Only problem now is the name of the file that's not respected.