I have a problem with displaying certain KMLs in Google Maps, it happens that after going through the method addLayerToMap, it is not rendered on the map.
Funny is that when I step it in Google MyMaps the same works normally and even if I export from there and set to display in Google Maps of the application, it displays normally.
I noticed that MyMaps greatly changes the structure of the KML and it is even smaller (in number of lines and consequently the size).
KML file (original): https://drive.google.com/file/d/1Z4AZMP1xNMgVNNXjK11-kD0gwlPLmJmR/view?usp=sharing
PS: On invalid paths of images, I changed manually and there were no results.
KML file (parsed by Google MyMaps): https://drive.google.com/file/d/1WPT3ZogzjTNa9ITeZze1cYf3ly4JFpUZ/view?usp=sharing
Method that I'm using to read KML (works with most of the KMLs I tried, including Google's own example):
private void retrieveFileFromResource() {
try {
KmlLayer kmlLayer = new KmlLayer(mMap, R.raw.teste3, getActivity());
kmlLayer.addLayerToMap();
moveCameraToKml(kmlLayer);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
I'm trying to add the components to the map manually (polylines, polygons, markers, etc) but did not succeed.
try using the below code to check if the kml file is present in the folder and if present, show them in the google maps.
private void retrieveFileFromResources()
{
try
{
int check = this.getResources().getIdentifier(teste3,"folder name", this.getPackageName());
if(check != 0)
{
InputStream inputstream = this.getResources().openRawResource(this.getResources().getIdentifier(teste3,"folder name",this.getPackageName()));
KmlLayer kmlLayer = new KmlLayer(mMap, inputStream, getApplicationContext());
kmlLayer.addLayerToMap();
}
else
{
Toast.makeText(this,"Request KML Layer not available",Toast.LENGTH_LONG).show();
}
}catch(IOException e)
{
e.printStackTrace();
}
catch(XmlPullParseException e)
{
e.printStackTrace();
}
}
Related
I'm developing a desktop application with java swing which should be able to display the visual content (notes, clefs, measures) defined in a MusicXML file in the frame. All .xml parsers that I found allow me to only create trees. I couldn't display the content with the JEditorPane, too.
Can I do it or will I need to first transform it dynamically to some other format such as .pdf? If so - how can I do it in java?
Use a JTextArea to let the user edit raw XML. Load the file in the background using SwingWorker, as shown here, to mitigate the effect of any latency.
#Override
protected Integer doInBackground() {
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("build.xml"));
String s;
try {
while ((s = in.readLine()) != null) {
publish(s);
}
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace(System.err);
} finally {
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
}
return status;
}
By visual representation, I meant that I needed a way to display this type of visual output.
You might look at the JMusicXML project, which has "some Java awt/swing graphics player." More resources may be found here.
A user is allowed to upload only image files to a servlet. After I temporarily store on the server, I read the file to make sure it is an image and maybe do some extra manipulations eg:
try {
InputStream input = uploadedFile.getInputStream()
if (ImageIO.read(input)!=null)
{
doStuff();
} else { // Not an image }
} catch (Exception e)
{
}
Is this vulnerable to any attacks? Or is this considered safe?
I am dynamically generating midi files (in cache dir) with an android app.
After generation, I play the file with MediaPlayer within the same app.
When running the app for the first time, it already needs the file to be there in the cache directory (the app crashes). It works on the emulator if I use the filemanager to put a dummy file there first. How can I circumvent this?
I need the app to run on a tablet for the first time, without requiring the file.
I am using these commands now:
try {
filePath = getCacheDir() + "/optimuse" + song + ".mid";
file = new File(filePath);
inputStream = new FileInputStream(file);
if (inputStream.getFD().valid()) {
System.out.println("Valid!");
}
} catch (Exception e1) {
e1.printStackTrace();
System.exit(-1);
}
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(inputStream.getFD());
inputStream.close();
} catch (Exception e1) {
e1.printStackTrace();
System.exit(-1);
}
try {
mediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Is there any way around this?
Thanks!
Maybe check whether the file exists before using it? You can achieve this using the File#exists() method.
First, you use the Context#getFileStreamPath(String) method - where the String is the filename of the file you are trying to access. Then you can call File#exists() on the returned object.
I am currently designing an application that needs to plot a route on a MapView. For this to function correctly I need to get data from a KML document that i get from:
http://maps.google.com/maps?f=d&hl=en&saddr=-33.882993,18.63486&daddr=-33.870162,18.657837&ie=UTF8&0&om=0&output=kml
I have created a test project to ensure that this data is received correctly. The problem is that it runs perfectly on my emulator, but not on the actual android phone.
The following piece of code starts a thread when a button is clicked, getting an input stream response (KML).
public void onClick(View v) {
if (v.getId()== R.id.btn1)
{
new Thread() {
#Override
public void run() {
String url = "http://maps.google.com/maps?f=d&hl=en&saddr=-33.882993,18.63486&daddr=-33.870162,18.657837&ie=UTF8&0&om=0&output=kml";
InputStream is = getConnection(url);
mRoad = RouteProvider.getRoute(is);
mHandler.sendEmptyMessage(0);
}
}.start();
}
}
private InputStream getConnection(String url) {
InputStream is = null;
try {
HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
conn.connect();
is = conn.getInputStream();
conn = null;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
For the moment, all getRoute() needs to do is give the contents of the "LineString" element inside the KML Document. These contents are a list of coordinates that can be used to draw a route.
public class RouteProvider {
/** Gets data from KML response **/
public static Road getRoute(InputStream is) {
Road road = new Road();
try
{
Document xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
NodeList a = xmlDoc.getElementsByTagName("LineString");
System.out.println(a.item(0).getTextContent());
road.mName = a.item(0).getTextContent();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return road;
}
On the emulator I am running the correct value is displayed within road.mName, which is a list of coordinates. On a Android phone it displays null?
I am building for Android 2.3.3 on eclipse and I used a Samsung Galaxy S2 for testing.
Did not need to use KML after all. Just used google maps to supply xml format. Much easier.
https://developers.google.com/maps/documentation/directions/
First off, I am not trying to write to the SDCard. I want to write some information to a file that persists between uses of the app. It is essentially a file to hold favorites of the particular user. Here is what the code looks like:
try {
File file = new File("favorites.txt");
if (file.exists()) {
Log.d(TAG, "File does exist.");
fis = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fis));
}
else {
Log.d(TAG, "File does not exist.");
return favDests;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
When running this code, we always get the "File does not exist." message in our DDMS log.
We have also tried the following code to no avail:
try {
File file = new File(GoLincoln.FAV_DEST_FILE);
fis = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fis));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
It is this second portion of code that results in the FileNotFoundException.
I have read multiple tutorials on writing and reading files on Android and I believe I am following them pretty closely, so I am not sure why this code doesn't work successfully. I appreciate any help!
You shouldn't use the File class directly. Use Activity.getCacheDir() to get the cache dir which is specific to your application. Then use new File(cachedir, "filename.tmp") to create the file.
Preferences and SQLLite will both allow you to have persistent data without managing your own files.
To use shared preferences you grab it from your context, then you edit the values like so
mySharedPreferences = context.getSharedPreferences("DatabaseNameWhateverYouWant", 0);
mySharedPreferences.getEditor().putString("MyPreferenceName", "Value").commit();
To get a preference out
mySharedPreferences.getString("MyPreferenceName", "DefaultValue");
This is really the simplest way to do basic preferences, much easier then doing a file. More then strings are supported, most basic data types are available to be added to the Preferences class.