uri to bitmap conversion fails [duplicate] - android

In my application I would like to use a gpx file downloaded from the server.
I use GPXParser from https://github.com/ticofab/android-gpx-parser .
When I was trying to parse gpx file, I got warring "java.io.FileNotFoundException: No content provider"
Gpx parsedGpx = null;
GPXParser parser = new GPXParser();
InputStream inputStream = getContentResolver().openInputStream(Uri.parse(stringUrl));
parsedGpx = parser.parse(inputStream);
How can I solve my problem?

getContentResolver is to get data from a ContentResolver not from any random Url, it is for getting data from other processes and usually takes the string form of content://....
You need to download the gpx first with something like HttpURLConnection and it's getInputStream() method to get an inputStream to use with the parser.

Related

download songs from website using json or jsoup

I want to buil a media player app in android studio
which can download songs from websites through API or using Jsoup
help me, please!
thank a lot!
Jsoup is not designed to download contents because its the parser but you can do one thing because there is one method that can get content in bytes then convert that bytes to any type according to your need
byte[] bytes = Jsoup.connect("your song url here").ignoreContentType(true).execute().bodyAsBytes();
and use following function to store above byte array as song file lolz
public void writeFile(byte[] data, String fileName) throws IOException
{
FileOutputStream out = new FileOutputStream(fileName);
out.write(data);
out.close();
}
i have not tested the code since testing is not friendly with me hope it will solve your problem

Getting file form Restlet Response

I created webapp which sends file by FileRepresentation. Client is an Android app. How can I get File from Restlet Response object on the client side?
The file content will be present within the payload. So you can extract it like any payload with Restlet, as described below:
ClientResource cr = new ClientResource(...);
Representation rep = cr.get();
In fact, the FileRepresentation class is provided in order to fill request / response from a file but can't be used to extract content of a response.
To have access to your response content on the client side, it depends on the file type. If you receive an ascii content, you can do something like that:
Representation representation = cr.get();
String fileContent = representation.getText();
If it's a binary file, you need to work with a stream, as described below:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
cr.get().write(outputStream);
byte[] fileContent = outputStream.toByteArray();
Hope it helps you,
Thierry

How to retrieve external XML files into android application

In my Android application, I want to retrieve an XML file from the internet and parse some tags in that file to my application. I got a working tutorial here which uses XMLResourceParser but that tutorial points out how to read an XML file from a local directory.
What are the method calls to retrieve an external XML file into my Android application?
XmlResourceParser xrp = this.getResources().getXml(R.xml.test);
You can get an InputStream to the XML file using the following code:
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection)connection;
httpConn.setDoInput(true);
httpConn.setRequestProperty("charset", "utf-8");
int responseCode = httpConn.getResponseCode();
if(responseCode != HttpStatus.SC_OK) {
InputStream xmlStream = httpConn.getInputStream();
// Pass the xmlStream object to a XPP, SAX or DOM parser.
}
create a new folder "raw" in "res", put external xml in raw folder
InputStream inputStream = getResources().openRawResource(R.raw.yourXMLname);
If by "external XML file" you mean loading an XML file from the internet, check out this article on HelloAndroid for how to download an URL to your device.
In terms of the actual XML parsing, I've found SJXP "Simple Java XML Parser" to be the simplest solution. It's lightweight, easy to use, and works using callback handlers. The website also has some sample code which is very helpful.
http://www.thebuzzmedia.com/software/simple-java-xml-parser-sjxp/

Read local compressed XML file(gzip) in Android application

I don't know how to get an InputStream(Read gzipped local xml file) from the locally stored gzip xml file.
employee.gz
If someone Can help I really appreciate. Thanks
This link works with zip. http://techdroid.kbeanie.com/2010/10/unzip-files-in-android.html
I am not sure if it will work with gz files, but you could give it a try. There's a documentation on GZIPInputStream class on the dev docs.
http://developer.android.com/reference/java/util/zip/GZIPInputStream.html
This piece of code works.
GZIPInputStream inputStream = new GZIPInputStream(new FileInputStream(new File(
"path to file")));
String str = IOUtils.convertStreamToString(inputStream);
I have used a util class which converts the input stream to a string. You might want to do the reading part manually.

Android Reading Text File Line-by-Line From Res/Raw In Emulator, InvocationException On OpenRawResource

I've found through research on google that that I can read a text file by storing it in my res/raw folder and then accessing it through getResources().openRawResource(R.raw.words);
In a class WordHelper I the constructor provides throws an InvocationException on this line of code:
istream = getResources().openRawResource(R.raw.words);
Which is the one most examples seem to use without a problem, I will then go on to do this
isreader = new InputStreamReader(istream);
myReader = new BufferedReader(isreader);
once everything is working and then use the readLine() method.
All descriptions of InvocationException such as getCause are null, I definately have the file in the res/raw/words.txt.
Thanks for reading.
I got the same problem and solved it using isreader = new InputStreamReader(istream, "UTF8");

Categories

Resources