How to decode a bitmap from a local file deployed with Android application - xamarin

I am going through http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/images/
and trying to get the Local Images working with Android however I am experiencing some issues when attempting to do something in normal monodroid just as a further test. I'm using a SharedProject, just for reference.
I have added the test image at Resources/drawable (test1.png), and also setting the Build Action as AndroidResource as it describes, and if I do the following in Xamarin.Forms it works:-
Xamarin.Forms.Image myimage = new Image();
myimage.Source = ImageSource.FromFile("test1.png");
However, if I try and retrieve the same file via the following it comes back as null.
Android.Graphics.Bitmap objBitmapImage = Android.Graphics.BitmapFactory.DecodeFile("test1.png")
Does anyone know why this is null and how to correct?

The Xamarin.Forms FileImageSource has a fallback mode as it covers two different scenarios.
First it will check whether the file exists in the file system via the BitmapFactory.DecodeFile.
Should the file not exist as specified in the File property, then it will use BitmapFactory.DecodeResource to try and load the resource content as a secondary alternative.
FileImageSource for Android therefore isn't only just for files that exist in the file system but also for retrieving named resources also.
This link indicates that Android Resources are compiled into the application and therefore wouldn't be part of the file system as physical files.

Since test1.png is a Drawable you should use BitmapFactory.DecodeResource ()

string scr = "#drawable/test1.png";
ImageView iv = new ImageView(context);
Bitmap bm = BitmapFactory.DecodeFile(src);
if (bm != null)
iv.SetImageBitmap(bm);

Related

Adding dynamic assets to Flutter app in runtime

I have Flutter app which needs to load dynamic assets and content which I want to save for later use. I know about Assets I can have in build time at the folder "assets/" inside the app.
I want to download content using ZIP files and unzip them to app local folder so they won't delete in the next app update.
what are the folders Flutter allows me to add assets to at runtime?
You cannot dynamically add assets to Flutter app at runtime and that is why Shared_Preferences package was developed by the official Flutter_Dev Team.
https://pub.dev/packages/shared_preferences
If you want to store a File instead of bits of information then refer to the below example code (For a Image File):
Future getImage(ImageSource imageSource) async {
// using your method of getting an image
final File image = await ImagePicker.pickImage(source: imageSource);
// getting a directory path for saving
final String path = await getApplicationDocumentsDirectory().path;
// copy the file to a new path
final File newImage = await image.copy('$path/image1.png');
setState(() {
_image = newImage;
});}
If your still want to somehow add/delete files dynamically then the answer is that it is practically not possible because assets weren't designed to dynamically store files.
One should only use assets to store files which shall remain common for all users.

Xamarin can not read the file

I'm trying to read a Json file for my mobile app in Xamarin Studio, but it throws me an error
can not find the file
While I try the same in Console Application every thing is all right.
I'm trying reading the file this way:
string Jason = File.ReadAllText(#"C:\Folder\file.txt")
This information is taken from Xam160 Class from Xamarin University. Within mobile application the platform generates a isolated folder area specifically for your App. These are the preferred locations to store files specific to your App with AppHome representing the Root of your sandboxed area.
Android - AppHome/files
iOS - AppHome/Lirbrary/[subdirectory]
Windows Phone - AppHome\Local
To Access these paths you can work with the Environment.SpecialFolder Enumeration to access these locations with the Environment.GetFolder() Method.
//Android
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
//iOS
string docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string libFolder = System.IO.Path.Combine(docFolder,"..","Library");
// Windows has different implementation. GetFolder Path with throw exception on windows.
string path = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
Each of these will get you access to the path you need to be able to read and write files locally on which platform you are on.

How to store the Local image path in sqlite or xml database in Xamrin.Forms

I have given task to store the image path in database either xml schema or sqlite database.I am working on Xamarin.Forms PCL project. I am very confused with how to store image path in database.because in android image should be placed in Ressource/drawble,ios image placed in Resources folder. I read this xamarin.forms documentaion. But not getting how to get path and store in database. Please anyone provide any resources or example.
Hope you have gone through this documentation and to distinguish between iOS and Android is also Given here in this documentation .
if (Device.OS == TargetPlatform.iOS) {
// Save in sqlite with iOS path
}if (Device.OS == TargetPlatform.Android) {
// Save in sqlite with Android path
}
Hope it helps.

Is it possible to upload image or file to SkyDrive fom Metro Style App?

Is it possible to upload image or file to SkyDrive fom Metro Style App?
I have already found how to browse the file from SkyDrive. But I haven't found regarding uploading file to SkyDrive. If you reply me, it will be very thankful..
I don't think the file picker method works unless the user has the desktop app installed.
You should use a Sharing contract. If you add a data file (Storage Item) to share, then SkyDrive will be listed as a share target and the user gets a UI where they can choose where in their SkyDrive they want to save. This is how I implemented it in my app.
For more info...
http://msdn.microsoft.com/en-us/library/windows/apps/hh771179.aspx
You can use FileSavePicker to save files. This will of course give the user a chance to select where he wants to save to local documents folder or sky drive. The user is in control.
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
savePicker.DefaultFileExtension = ".YourExtension";
savePicker.SuggestedFileName = "SampleFileName";
savePicker.FileTypeChoices[".YourExtension"] = new List<string>() { ".YourExtension"};
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
await FileIO.WriteTextAsync(file, "A bunch of text to save to the file");
}
Please note that in the sample code I am creating the content of the file in code. If you want the user to select an existing file from the computer then you will have to first use FileOpenPicker, get the file and then use FileSavePicker to save the contents of the selected file to the SkyDrive
Assuming that you are using XAML/JavaScript, the suggested solution is to use FilePicker.
The following link may help you.
http://msdn.microsoft.com/en-us/library/windows/apps/jj150595.aspx
Thanks Mamta Dalal and Dangling Neuron, but there is problem. But it looks like I can't use FileSavePicker. I have to upload file(documnet, photo) not only text file. I have to copy from one path to another. If I use FileSavePicker, I have to write every file content (text, png, pdf, etc) and can't copy. Currently I am using FolderPicker. But unfortunately, FolderPicker doesn't support SkyDrive.My Code is As follow:
>FolderPicker saveFolder = new FolderPicker();
>saveFolder.ViewMode = PickerViewMode.Thumbnail;
>saveFolder.SuggestedStartLocation = PickerLocationId.Desktop;
>saveFolder.FileTypeFilter.Add("*");
>StorageFolder storagefolderSave = await saveFolder.PickSingleFolderAsync();
>StorageFile storagefileSave = [Selected storagefile with file picker];
>await storagefileSave.CopyAsync(storagefolderSave,storagefileSave.Name,NameCollisionOption.ReplaceExisting);
It will be greate that if FolderPicker supports SkyDrive or can copy file using FileSavePicker.

How to access local media file on my computer in JavaFX?

How to access local media file on my computer in JavaFX?
Here are the urls I tried:
C:/PROJECT/videos/on2tm_352.flv
file:///C://PROJECT/videos/on2tm_352.flv (suggested in some site forgot where)
It does play however, when I put the media file inside the project's folder and access it using {__DIR__}/on2tm_352.flv
Note: There are no exceptions and errors outputted. The screen is just blank.
KLite Codec 583 Mega, JavaFX 1.2, Netbeans 6.8 are used
It is working right now for me:
private static final String MEDIA_URL = "file:/c:/Users/Alejandro/Downloads/oow2010-2.flv";
I tested it a few minutes ago....
or something like that:
private File file = new File("c:/Users/Alejandro/Downloads/oow2010-2.flv");
private final String MEDIA_URL = file.toURI().toString();
See you later =D
Try this out:
Media media = new Media(trackFile.toURI().toURL().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
By giving their URL to the Media?
Note that some issues with spaces in paths have been reported in the past, I don't know if it is still true.
[EDIT following original message edit (URL examples)]
First line isn't an URL, it is a path. Apparently the media player accepts paths as URL, but that's not the case for ImageView, though, so it is better to be strict.
Second line is correct.
Third line have a potential issue: __DIR__ variables has already a terminal slash, so you should not add it, ie. write {__DIR__}on2tm_352.flv instead. Not sure if that's the issue (I haven't used much video yet) but worth trying.
Note that such URL (based on __DIR__) will point inside a jar file once the project is packaged.
It is OK in JavaFX 1.2, but for some odd reason, they chose to disallow such access in 1.3.
I have found it easier to do the following with disk files. This relieves my feeble brain of determining all the rules for "file:" urls:
var file = new File("C:/PROJECT/videos/on2tm_352.flv");
Media {
source: "{file.toURI()}"
}
I avoid using {__DIR__} for media as it can point to a "jar:" URL and that is no longer supported for media locations in JavaFX 1.3.
You guys just have to specify the file's path as an URI path:
Media media = new Media("file:///C:/Users/David/Downloads/test.flv");
MediaPlayer mediaPlayer = new MediaPlayer(media);
It's not required to instantiate a File at all.

Resources