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
Related
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.
I wrote an android application that part of it is to handle upload and download documents. Currently I am using the Microsoft Azure server to save the files on.
The way I am currently doing it is by turning the files to a string and saving it that way on the Azure server:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis;
try {
fis = new FileInputStream(new File(Uridata.getPath()));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
} catch (Exception e) {
e.printStackTrace();
}
byte[] bbytes = baos.toByteArray();
item.setStringFile(Base64.encodeToString(bbytes, Base64.URL_SAFE));
item.setName(Uridata.getLastPathSegment());
where item is my class that saves the string representation and the name of the file and is being loaded to the Azure, Uridata is an Uri instance of the file chosen.
I have one main problem with this solution and it is the limit on the file size.
I am searching for a good server to use instead of the Azure (maybe a RESET one) and if there is a better way to save files of all kinds (pdf, word...).
I will also want in the future to use the same data in a web interface
Does anybody have any suggestions on how to do it?
Thanks in advance!
To start, you don't have to transform the file into a string, you can just save it as a file. You have the possibility of losing data by continuing to do that. See: How do I save a stream to a file in C#?
If you're looking for another service to save files, then you should look into Azure Blob Storage. It will allow you to upload as much data as you want to a storage service for arbitrary files. See for example:
https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/
With the Google Photos app, I am trying to pick a video, that is not cached on the device.
I am using the ACTION_GET_CONTENT intent, to launch the options dialog, and from there I choose the Google Photos app.
While selecting local videos, it returns an Uri in this form.
content://media/external/video/media/6708
And from that, I query the content provider to retrieve the actual file location, and proceed from there. The file location looks like this.
/storage/emulated/0/WhatsApp/Media/WhatsApp
Video/VID-20131102-WA0000.mp4
Now, when I choose an online video, i.e: a video not available on my device yet, and which needs to be downloaded to be used, the returned Uri looks like this:
content://com.google.android.apps.photos.content/1/https://lh6.googleusercontent.com/_RD-QTO_SK5jlaPldTe2n5GANqMc3h-ukcbNoFlF1NLy=s0-d
Now, with this, there is no documented ContentProvider that would help me to get the actual link to this video. Even if I do a query, it returns nothing apart from a DISPLAY_NAME and SIZE columns.
DISPLAY_NAME contains video.mpeg (Same display name for different videos)
SIZE probably tells me the size of the actual file.
Referred to this post on SO.
I checked various posts, and thought that I would have to get an InputStream for the video through the content provider, save the file, and work with the file. Picking an image file however works fine, but with video it doesn't.
So, to copy the stream to a file, I have this code.
InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse(path));
And finally write to a temporary file. The file gets created, but that doesn't seem to be correctly formatted. VLC plays the file, but shows only the first frame all throughout.
If I take the URL from the last part of the URI given above, and try to view it on a browser, it downloads a GIF file. I am guessing that's the problem. But I don't know how to get the mpeg format of the video.
Anyone has experienced the same?
Finally found the solution to the problem. This would work for both images and videos.
Referenced this video:
DevBytes: Android 4.4 Storage Access Framework: Client
https://www.youtube.com/watch?v=UFj9AEz0DHQ
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivity(intent);
Get the Uri, and access and save the file like this:
ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver()
.openFileDescriptor(Uri.parse(path),"r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
InputStream inputStream = new FileInputStream(fileDescriptor);
BufferedInputStream reader = new BufferedInputStream(inputStream);
// Create an output stream to a file that you want to save to
BufferedOutputStream outStream = new BufferedOutputStream(
new FileOutputStream(filePath));
byte[] buf = new byte[2048];
int len;
while ((len = reader.read(buf)) > 0) {
outStream.write(buf, 0, len);
}
For some reason, getting an input stream without using a ParcelFileDescriptor doesn't work.
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.
I'm playing with android and trying to receive file (pdf book). To do this I wrote Servelt that encode the book into base64 string and put it in the XML document. That document contains name, author and IBSN fields also. I successfully receve it. In case of a small book I can even decode it and open. But if the size is more than 2mb I get OutOfMeoryError.
My parser code is:
public void characters(char[] ch, int start, int length)
throws SAXException {
if(builder==null) builder = new StringBuilder();
builder.append(new String(ch, start, length));
Then I do the following:
fos = new FileOutputStream(Environment.getExternalStorageDirectory().toString()+"/book.pdf", false);
byte[] toWrite = Base64.decode(builder.toString(), Base64.DEFAULT);
fos.write(toWrite);
fos.flush();
fos.close();
Does anybody know how can I parse it wothout error? I've tried to parse it in
characters method (I mean using small buffer) but it fails... Illegal base 64 string.
Does anybody know how can I parse it wothout error?
You can't, most likely, as there is not enough RAM for you to work with.
Sensible programmers would not convert a large binary file into "base64 string and put it in the XML document". Sensible programmers would:
put the metadata (name, author, ISBN) in property fields within the PDF, which is a fine solution for Java, but may be a problem for Android, as the libraries to get at those fields may or may not work (e.g., iText); or
download the two files separately (one with the XML of metadata, which contains the URL to the PDF file); or
package the PDF and the XML file into a single archive (e.g., ZIP) and download the archive
There may be other solutions than those three, but they should give you a starting point.