Can't play this video Xamarin.Android - c#

I need to play video from sdcard on VideoView and always getting "Can't play this video message". I am testing on physical device. File is on external storage, and can be played by build in phone apps with no problem.
I have read some topics before and tried changing file format and resolution with no effect.
Second guess was path to the file so tried to change it multiple times in many variants using Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, creating File variable and getting path from File.Path.
Got the TextView where I put path every time and it is always correct.
I have no idea why it does not work.
Code:
Java.IO.File file = new Java.IO.File (Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/myimage/", "video.mp4");
tView.Text = file.Path;
MediaController mediaController = new MediaController(context: this);
vView.SetMediaController(mediaController);
mediaController.SetAnchorView(vView);
vView.SetVideoPath(file.Path);
vView.Start();
File.Path value:
"/storage/emulated/0/myimage/video.mp4"
EDIT: I have created "raw" folder in resource putted file there and then used following path:
vView.SetVideoPath("android.resource://" + PackageName + "/" + Resource.Raw.video);
It works, but if anyone know how to get the proper path to folder on sdCard?

Finally I figured it out. The problem was that my phone mounted card at path /storage/xxxx-xxxx/ my research revealed that the dir's name come from volume serial number(where android automatically mount card) and ExternalStorageDirectory refers to other locations in each android device. Sometimes it's internal sometimes usb mass storage.
Anyway didn't manage to find any method that returns the path so created following method:
string GetCardMountPoint()
{
string[] listOfDirs = Directory.GetDirectories("/storage/");
string path = null;
foreach ( string dir in listOfDirs)
{
if(dir.Contains('-'))
{
path = dir;
}
}
return path;
It's not much of sophisticated solution but it works for now.

Related

Locating file path of image in android devices

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.

Removable SD External Storage filepath needed in Android code

I've been working on a basic music player app.
I am trying to keep questions concise and so not put too much code or irrelevant info.
Basically, I want to add all mp3/m4a/wma to a long List of Strings. But no matter what methods I call from different libraries I cannot get it.
Here's what I've ended up with:
string musicPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMusic).AbsolutePath;
string musicPath_sd = GetExternalFilesDir(Android.OS.Environment.DirectoryMusic).ToString();
Log.Info("FILEPATHS TAG", "phone memory music directory path is: " + musicPath);
Log.Info("FILEPATHS TAG", "sdcard music directory path is: " + musicPath_sd);
So the string musicPath is fine and returns:
Time Device Name Type PID Tag Message
FILEPATHS TAG phone memory music directory path is: /storage/emulated/0/Music
FILEPATHS TAG sdcard music directory path is: /storage/emulated/0/Music
As you can see the 'sdcard music directory' is returnign the exact same location, but really it should be: storage/**extSdCard**/MUSIC (I know this because I browsed to the songs on my phone in EZ File Explorer and checked the info button which displays the path.
I am using Samsung Note 3 on Android 5.0 API 21 as physical debugging device

Xamarin media plugin get android image path

I am using the media plugin for xamarin forms (by james montemagno) and the actual taking of the picture and storing it works fine, I have debugged the creation of the image on the emulator and it is stored in
/storage/emulated/0/Android/data/{APPNAME}.Android/files/Pictures/{DIRECTORYNAME}/{IMAGENAME}
however in my app it will get a list of file names from an API I want to check if the image exists in that folder.
The following works fine on IOS
var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string jpgFilename = System.IO.Path.Combine(documentsDirectory, App.IMAGE_FOLDER_NAME);
jpgFilename = System.IO.Path.Combine(jpgFilename, name);
I have tried the 2 following methods for getting it on android but both are incorrect
var documentsDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string jpgFilename = System.IO.Path.Combine(documentsDirectory, App.IMAGE_FOLDER_NAME);
jpgFilename = System.IO.Path.Combine(jpgFilename, name);
Java.IO.File dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DataDirectory + "/" + App.IMAGE_FOLDER_NAME + "/" + name);
dir ends up as
/storage/emulated/0/data/{DIRECTORY}/{IMAGENAME}
jpgFileName ends up as /data/data/{APPNAME}.Android/files/{DIRECTORYNAME}/{IMAGENAME}
I dont want to hardcode anything in the paths but neither of these are right. I could not find anything in the GIT documentation for getting the file path except by looking at the path of the file created when taking a picture
The problem
I had the same kind of issue with Xamarin Media Plugin. For me, the problem is:
we don't really know where the plugin save the picture on android.
After reading all documentation I found, I just noted this:
... When your user takes a photo it will still store temporary data, but also if needed make a copy to the public gallery (based on platform). In the MediaFile you will now see a AlbumPath that you can query as well.
(from: Github plugin page)
so you can save your photo to your phone gallery (it will be public) but the path is known.
and we don't know what means "store the temporary data".
Solution
After investigating on how/where an app can store data, I found where the plugin stores photos on Android >> so I can generate back the full file names
In your Android app, the base path you are looking for is:
var basePath = Android.App.Application.Context.GetExternalFilesDir(null).AbsolutePath
It references your app's external private folder. It looks like that:
/storage/emulated/0/Android/data/com.mycompany.myapp/files
So finally to get your full file's path:
var fullPath = basePath + {DIRECTORYNAME} + {FILENAME};
I suggest you to make a dependency service, for instance 'ILocalFileService', that will expose this 'base path' property.
Please let me know if it works for you !
I resolved a similar problem. I wanted to collect all files for my app in a folder visible to all users.
var documentsDirectory = Android.OS.Environment.GetExternalStoragePublicDirectory(
Android.OS.Environment.DirectoryDocuments);
If you want to create Directory, add to your class using System.IO; and you have the same functions in a normal .NET application.
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
If you want to create files or directory, you can use PCLStorage.
public async Task<bool> CreateFileAsync(string name, string context)
{
// get hold of the file system
IFolder folder = FileSystem.Current.LocalStorage;
// create a file, overwriting any existing file
IFile file = await folder.CreateFileAsync(name,
CreationCollisionOption.ReplaceExisting);
// populate the file with some text
await file.WriteAllTextAsync(context);
return true;
}
to get the private path
var privatePath = file.Path;
to get the public Album path
var publicAlbumPath = file.AlbumPath;
se the documentation here https://github.com/jamesmontemagno/MediaPlugin

Can't find path on android

I have been searching for almost a day now and this matter is driving me crazy.
I am trying to open a connection to a SQLite DB I push to the device from windows.
The database now is inside this path:
Phone/Android/data/MyApp.Droid/files/MyDB.db3
which can be read without 'Phone/Android' I think.
When I try to make a connection to the DB I can't seem to find the path.
Here's my code for retrieving the path and creating the connection.
public SQLite.SQLiteConnection GetConnection()
{
var sqliteFile = "MyDB.db3";
var dataPath = global::Android.OS.Environment.DataDirectory.AbsolutePath;
var dataPathExtension = #"MyApp.Droid/files";
var destination = Path.Combine(dataPath, dataPathExtension, sqliteFile);
//this outputs the following: /data/MyApp.Droid/files/MyDB.db3
//When I check my phone this is exactly when I can find the file.
return new SQLite.SQliteConnection(destination);
//It can't find the path/file.
}
The DB needs to be in a location I can access from windows without rooting the device.
Can anyone explain to me why it can not find the path/file and if possible tell me how I can read the location?
Note: I cannot access any 'Environment.SpecialFolder' from windows it seems as this gives me a path like: data/user/0/MyApp.Droid/files/
Yours,
I use this implementation:
public SQLiteAsyncConnection GetConnection()
{
var fileName = "DatabaseName.db3";
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsPath, fileName);
var connection = new SQLiteAsyncConnection(path);
return connection;
}
Then I'll grab the .db3 file using Android Device Monitor and the File Explorer inside. You can then open that .db3 file with http://sqlitebrowser.org/ or another browser that supports SQLite. Please note: It's easiest on an Emulator as pulling the files from a physical device can be quite cumbersome without root.
Open Tools -> Android -> Android Device Monitor:
Run your Application in Debug mode
Select your Device in the Devices section:
Pull your .db3 file from data/data/com.mypackage.name/files via the Save Icon:
Open the .db3 file in a SQLite browser

How to get the music folder path in xamarin.android?

I'm trying to save a mp3 file, into the device's music library folder.(I Want it to show up immediately as a playable song).
More clearly i'm looking for the path /storage/emulated/0/Music/, in each phone.Im not sure if this path changes, so i would rather not take the risk.
I have tried this paths:
System.Environment.SpecialFolder.CommonMusic -> Blank
System.Environment.SpecialFolder.MyMusic -> /data/user/0/App.App/files/Music
Android.OS.Environment.DirectoryMusic -> /Music
None of this offer, what i wanted.I tried writing to the /storage/emulated/0/Music/ which did the job, but i dont think this path is stable.
Anyone knows how can i get the music folder path programmatically ? Im coding in C# using xamarin.android.
I used this code to get access to my Music Folder
public void StartPlayer(String filePath)
{
string Mp3Path = Path.Combine("/sdcard/"+
Android.OS.Environment.DirectoryMusic,
filePath);
if (player == null)
{
player = new MediaPlayer();
}
player.Reset();
player.SetDataSource(Mp3Path);
player.Prepare();
player.Start();
}
The sdcard shortcut works great for me, hope it works for you too ^^

Categories