2222 2222 - 11 months ago 155
Android Question

Import a file during runtime in Android using Unity3D

I'm trying to import a file during runtime. I put a file

f8.csv
in this location on android
Internal storage\Android\data\com.site.myapp\files


The user types the file name in the input field, I get the file name and then
Then I try to access it using this code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using System;
using System.IO;

public class ReadAFile : MonoBehaviour
{
public InputField fileName;
public static volatile string file_name;
FileInfo f1;
public static volatile string message1, message2, message3, path;
public static volatile string file_content;



public void MyLoad()
{
file_name = fileName.text.ToString();
f1 = new FileInfo(Application.persistentDataPath + "\\" + file_name);
if (f1.Exists)
{

StreamReader r = File.OpenText(Application.persistentDataPath + "\\" + file_name);
string info = r.ReadToEnd();
r.Close();
message1 = "Content of the file is: " + info;
}
else
{
message1 = "File not found!";
}
}


I don't know what I'm doing wrong but this doesn't work. It doesn't work with a text file. It doesn't work if I change
persistentDataPath
to just
dataPath

Answer Source

Was able to achieve what I needed using WWW. I changed the location of my text file as well.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;


public class ImageRead : MonoBehaviour {

    string url;

    public void readmyurl()
    {

           url = "file:///sdcard/Android/data/f8.txt";


    }

    private void Start()
    {

        StartCoroutine(file_read());

    }

    IEnumerator file_read()
    {
        readmyurl();
        yield return 0;
        WWW imgLink = new WWW(url);
        yield return imgLink;
        message1 = imgLink.text; //message1 is the content of file f8

    }



}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download