I have Json file (test.txt) and I wanna get data from that file to Android App. My code is:
private static String url = "file:///AndroidJSONParsingActivity/res/raw/test.txt";
But it is not working. Error I get is:
error opening trace file: No such file or directory (2)
Somebody help me? Thanks!
Put the test.txt file into assets folder
public class Utility {
public static String readXMLinString(String fileName, Context c) {
try {
InputStream is = c.getAssets().open(fileName);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String text = new String(buffer);
return text;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Then you can get test.txt using the following code
JSONObject json = new JSONObject(Utility.readXMLinString("test.txt",getApplicationContext()));
You have an answere Using JSON File in Android App Resources
You need to put the file intro raw folder and you can acces with this:
getResources().openRawResource(resourceName)
use getResources().openRawResource(RawResource_id) for reading an text file from res/raw folder as:
InputStream inputStream = Current_Activity.this.
getResources().openRawResource(R.raw.test);
//put your code for reading json from text file(inputStream) here
use following code to open input stream for the file stored in the raw folder:
getResources().openRawResource(R.raw.text_file)
Please see below code for that, it will solve your problem.
public void mReadJsonData() {
try {
InputStream is = getResources().openRawResource(R.raw.textfilename)
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String mResponse = new String(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Related
I've created an app on Android Studio with Libgdx that can read a .json file that is stored in the project folder
it goes like this
private void readFile() {
JsonParser jsonParser = new JsonParser();
try (FileReader reader = new FileReader("file.json"))
{
//reads file
} catch (IOException | ParserException e) {
e.printStackTrace();
}
}
Everything works on the desktop application, but when I run it on android app ofcourse its not there and it doesn't load. I can't find any information to where it has to be stored. FileReader only lets me input a string, but not the location where the file is.
Is FileReader a bad choice or the location where it is looking can be changed?
You need to store you json file in assets folder. And to read your .json from assets file use below code.
JSONObject obj = new JSONObject(readJSONFromAsset());
public String readJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("yourFile.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
I need to read "strings.json" file that lies in the "assets" folder of my Android project. But
File file = new File(filepath);
Logger.e(file.exists() ? "exists" : "doesn't exist");
says that the file doen't exist. I've tried the following variants of the filepath:
strings.json
android_asset/strings.json
/android_asset/strings.json
file:///android_asset/strings.json
What's wrong?
For read files in Assets:
InputStream is = context.getAssets().open("strings.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
fileResult = new String(buffer, "UTF-8");
In fileResult you should retrieve the content of your file.
AssetManager manager = getAssets();
try {
InputStream stream = manager.open(string+".xml");
} catch (IOException e) {
e.printStackTrace();
}
I have Kml file which have some lat lngs but I don't know how to parse it in android?
I checked this link
How to draw a path on a map using kml file?
but it's different. It is getting the data from the server in kml format. But I have kml file stored in assets.
Thanks in advance..
Read KML:
public String loadKMLFromAsset() {
String kmlData = null;
try {
InputStream is = getAssets().open("yourKMLFile");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
kmlData = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return kmlData;
}
So this is a pretty embarrassing question, but i have a text file and java will read all of the words in it and add it to a array, i don't know where to put the text file, like what folder so the comp can go get it? could someone tell me. my code works in a regular java application, so it should work on android.
you can use
<your-context>.getAssets();
to return an AssetsManager object.
AssetsManager assets = context.getAssets();
You can then open an input stream with the open() method.
InputStream inputStream = assets.open("filename");
The InputStream object is a standard Java object from the IO package. You can decorate this stream with an object decorator you wish (Reader, BufferedReader, etc).
If you wish to move this file out of the APK (that is not inflated) to the phone you can just copy the bytes of the file from the input stream using an output stream. Note you will have to have permissions in your write directory (you can do this if your phone is rooted and you have created a shell interface to run native shell commands through JNI).
UPDATE
try {
InputStream inputStream = this.getAssets().open("test.txt");
BufferedReader buffer = new BufferedReader(new Reader(inputStream));
String line;
while((line = buffer.readLine()) != null) {
tots.add(line);
}
}
catch(IOException e) {
e.printStackTrace();
}
Haven't tested it, but I think this is what you want.
You can put the file to assets folder and use
InputStream stream = getAssets().open(filename);
to get the input stream
I created new raw folder in res folder and put chapter0.txt in here.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.induction);
wordss = new Vector<String>();
TextViewEx helloTxt = (TextViewEx) findViewById(R.id.test);
helloTxt.setText(readTxt());
}
private String readTxt() {
InputStream inputStream = getResources().openRawResource(R.raw.chapter0);
// getResources().openRawResource(R.raw.internals);
System.out.println(inputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1) {
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
I have a path stored in my database to a file text1.txt, which contains the text I want to display. The file is in the assets folder, assets/text1.txt.
How do I open this file and display its content?
The code is:
if (placetext != null) {
try
{
InputStream textpath = getAssets().open(text);
//Bitmap bit = BitmapFactory.decodeStream(textpath);
placetext.setText(text);
//placetext.setText(text);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
...and the view on the emulator is just text1.txt not the content of the file.
i already have the solution
String text12 = b.getString("texts")
try {
InputStream is = getAssets().open(text12); //
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String text= new String(buffer);
placetext = (TextView)findViewById(R.id.detailText2);
placetext.setText(text);
} catch (IOException e) {
throw new RuntimeException(e);
}
That's normal because :
InputStream textpath = getAssets().open(text);
I guess here : text represent the name of your file to open.
placetext.setText(text);
That put the text passed in parameter in the textfield, so right now the name of your file.
To put the content of your file in the textfield, you have to open the file, read it and store the content in a StringBuffer and after put the StringBuffer content in the textfield.
EDIT :
StringBuilder text = new StringBuilder();
Scanner scanner = new Scanner(new FileInputStream(new File('yourfile')));
try {
while (scanner.hasNextLine()){
text.append(scanner.nextLine());
}
}
finally{
scanner.close();
}
}
One solution among a lot of others to read the content of a file in Java.
Hope it helps
This is what I use to read the contents of a XML stored in /assets folder:
public static String getXMLFromAssets(Context ctx, String pathToXML){
InputStream rawInput;
//create a output stream to write the buffer into
ByteArrayOutputStream rawOutput = null;
try {
rawInput = ctx.getAssets().open(pathToXML);
//create a buffer that has the same size as the InputStream
byte[] buffer = new byte[rawInput.available()];
//read the text file as a stream, into the buffer
rawInput.read(buffer);
rawOutput = new ByteArrayOutputStream();
//write this buffer to the output stream
rawOutput.write(buffer);
//Close the Input and Output streams
rawOutput.close();
rawInput.close();
} catch (IOException e) {
Log.e("Error", e.toString());
}
//return the output stream as a String
return rawOutput.toString();
}