How can I parse kml file from the assets folder? - android

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;
}

Related

Where to store .json file in Android Studio project so i could read it with FileReader?

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;
}

How to open .docx file(from assets) in android TextView in proper format?

With this code, I am able to get the text file. If I replace text file with docx file it shows data in Encoded form and I need to show the text in proper format. Is it possible with .docx file ? or any other solution for that.
try {
InputStream is=getActivity().getAssets().open("getting_started.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a string.
String text = new String(buffer);
// Finally stick the string into the text view.
tv_help_text.setText(text);
} catch (IOException e) {
throw new RuntimeException(e);
}

How do I address files in Asset folder?

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();
}

Android get data from json file (.txt)

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();
}
}

How to open a file in the assets folder using a path stored in the database?

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();
}

Categories

Resources