I would like to set source of my image object programmatically in Xamarin. I have a text file and it has more than 200 file names. I read it line by line. So see what i want to do (not in Xamarin.Forms)
string[] MyFileNames = new string[] { };
MyFileNames = ReadedTextFileVariable.Split(System.Environment.NewLine);
ImageObject.ImagePath = MyFileNames[2]; //its an example. i want to put a string path. uri is not working
Thanks in advance.
Related
In my Unity3D project I have several text fields. I saved my text in some text files.
When I test my project on the computer everything works fine, and my code reads the text files. But if I upload to my iPad it won't work and the text fields stay empty.
In the image you can see where I have saved my text files.
To read my text files I use the following code:
public Text infoText;
void Update()
{
readTextFile("FileName", "StepNumber")
}
public void readTextFile(string fileName, string stepNumber)
{
StreamReader txt_Reader = new StreamReader("Assets/Resources/Text_Files/" + fileName + ".txt");
while(!txt_Reader.EndOfStream)
{
string txt_String = txt_Reader.ReadLine();
if(txt_String.Contains(stepNumber))
{
string[] separator = { "_" };
string[] strList = txt_String.Split(separator, System.StringSplitOptions.RemoveEmptyEntries);
infoText.text = string.Join("\n", strList.Skip(1));
}
}
}
What do I have to change that my iPad can read from the text files?
EDIT:
My text files looks like this:
Step 1:
* Some Text
* Some Text
Step 2:
* Some Text
* Some Text
* Some Text
Step 3:
* Some Text
Step 4:
* Some Text
So each * should be a new line in my text field. With my old c# code this was no problem, but with
var lines = textFiles.text.Split(new char[] { `*` });
foreach(var line in lines)
{
...
}
i do not know how I can do that, that my text field shows all two lines for step one.
First of all from the Best Practices for the Resources folder
**Don't use it!
Please read the reasons there.
In general for system paths do never use simple string concatenation + "/" +!
Rather use Path.Combine which automatically uses the correct path separators according to the executing platform
Path.Combine(Application.dataPath, "Resources", "Text_Files", fileName + ".txt");
However, you don't/can't simply use a StreamReader to access the Resources folders (See Resources API since it is packed into the build so you have to go through Resources.Load like
// Here you can use / since this is how Unity stores internal paths
// for load you omit the suffix
TextAsset textFile = Resources.Load<TextAsset>("Text_Files/" + filename);
string fileContent = textFile.text;
Or also have a look at Resources.LoadAsync to not block the main thread meanwhile.
BUT
Speaking about blocking the main thread: What you definitely do not want to do is using any of these within Update thus doing heavy FileIO/Loading every frame!
Store the content of that file once as it won't change afterwards anyway!
Depending on your needs you could also simply put your file in any other folder inside the Assets and simply use a TextAsset field directly and drag it into according slot via the Inspector
public TextAsset textFile;
Finally you can then go through the lines one by one using e.g.
var lines = textFile.text.Split(new char[]{'/n'});
foreach(var line in lines)
{
...
}
Note that also that Split is a quite heavy operation since it has to parse every single character in the string and create new substrings so even store these results somewhere in a field of you need them multiple times during runtime!
Typed on smartphone but I hope the idea gets clear
In your case, StreamReader txt_Reader = new StreamReader("Assets/Resources/Text_Files/" + fileName + ".txt"); points to a file on your computer. Assets/Resources/Text_Files/ only exists on your computer.
You need to access a folder that exists on your iPad. It's likely you also didn't save your data to a folder existing on your IPad.
For other devices you could use : Application.persistentDataPath + "/" + fileName
Source: https://docs.unity3d.com/ScriptReference/Application-dataPath.html
I am trying to load an image to picture box the below way worked with me.
image1.Image = Properties.Resources._0;
what it i want make the code reading from resources as variable name like the way below.
var imagename = _0;
image1.Image = Properties.Resources.imagename;
Thanks
Use the ResourceManager and pass it a string:
var imageName = "_0";
image1.Image = (Image)Properties.Resources.ResourceManager.GetObject(imageName);
Basically I am trying to get the details of a music file inside an android device (Ex. The music's title, artist, etc) and store them to a list<>. (SongData contains the list, and it contains a list of Song object.)
public void PopulateSongList(Context context) {
SongData songData = new SongData();
Android.Net.Uri musicUri = Android.Provider.MediaStore.Audio.Media.ExternalContentUri;
//Used to query the media files.
ICursor musicCursor = context.ContentResolver.Query(musicUri, null, null, null, null);
if (musicCursor != null && musicCursor.MoveToFirst())
{
int songID = 0;
//get columns.
int songTitleColumn = musicCursor.GetColumnIndex("title");
int songArtistColumn = musicCursor.GetColumnIndex("artist");
//add songs to list
do
{
String songTitle = musicCursor.GetString(songTitleColumn);
//Tried, but file name does not contain file extension.
if (songTitle.ToLower().Contains(".mp4") || songTitle.ToLower().Contains(".m4a"))
{
String songArtist = musicCursor.GetString(songArtistColumn);
songData.AddNewSong(new Song(++songID, songTitle, songArtist, null));
}
}
while (musicCursor.MoveToNext());
}
}
The code works well but however in the do..while loop, songTitle also contains other files that are not music files as well.
I tried checking for file extensions but it does not work, and after using the debugger again, the file name given does not contain the file extension.
So how do I make sure that it only grabs details from a music file, and not other kind of files?
(Also, I want to grab the name of the artist of a music file, but apparently int songArtistColumn = musicCursor.GetColumnIndex("artist"); does not seem to work.)
Finally, how do I get the image of a music file? (The song image).
You can try to use musicCursor.GetColumnNames() to see what the file is allowing you to work with.
Here's an image of the possible results you can get if you execute it.
musicCursor.GetColumnNames() will return the results in a string[] so you can use List<string> songInfo = new List<string>(string[]);
A problem you might get is a NullException, this happens when the data you're trying to work with have a value of null. You'll get this value directly from the file if the Artist or other values are unknown.
I don't know where the Image file is stored, but if it's stored with the music file it will most likely be stored in a byte[]. So you'll need to convert the byte[] to an image.
If I have some resources, eg card1.png, card2.png, etc,
I want to them load them into a picBox, but only load the correct image
eg, something like
int cardNum = rn.Next(0,cardMax);
picCard.Image = Properties.Resources."card"+cardNum+".png";
Obviously that doesn't work, but how would I do what I am trying to do (load the correct resource after building the resource name into a string)
Instead of the generated properties in the Resources class use the ResourceManager directly:
string resName = $"card{cardNum}.png"; // Check the correct name in the .resx file. By using the wizards the extension is omitted, for example.
picCard.Image = (Image)Properties.Resources.ResourceManager.GetObject(resName);
Try using:
var rm = new System.Resources.ResourceManager(((System.Reflection.Assembly)System.Reflection.Assembly.GetExecutingAssembly()).GetName().Name + ".Properties.Resources", ((System.Reflection.Assembly)System.Reflection.Assembly.GetExecutingAssembly()));
Image img = (Bitmap)rm.GetObject("resourcesimagename.jpg");
I am creating application. In that, I put combo box, combo box filled by application no. came from db. on the selection of combo box the grid fill that files which in another machine in drive. Like //user/public/af001/edit/test.JPEG and in grid I put the download link on particular row for download that file in my machine but issue is when I am downloading that file, I am not geting //user/public/af001/edit/test.JPEG.
if (e.Column Index == 0)
{
int row;
//Get the row index
row = e.Row Index;
string old Path = #"~\\Users\Public\AS\AFS1402190001\Edit\test.JPEG";
string new path = #"E:\example\";
string new File Name = "new file name";
File Info f1 = new File Info(old Path);
if (f1.Exists)
{
if (!Directory.Exists(new path))
{
Directory.Create Directory(new path);
}
f1.Copy To(string.Format("{0}{1}{2}", new path, new File Name, f1.Extension));
}
}
Can you tell me what is the issue?
Thanks for help.
The problem is at this line:
string.Format("{0}{1}{2}", new path, new File Name, f1.Extension);
it looks you are not building a correct file patch. try to use System.IO.Path.Combine to combine directory and fileName. something like:
System.IO.Path.Combine(dirPath, string.Format("{0}.{1}", fileName, extension));
of course, you might want to make sure the path/filename does not have invalid chars, you can use System.IO.Path.GetInvalidFileNameChars() and System.IO.Path.GetInvalidPathChars() to get those illegal chars that you need to avoid.