I want to get the video size in Mega Bytes from a Url in an android app. I have a Player that plays a video inside an app. I want to display next to my player the current videos size. So an example would me 23mb. If the video is 23 mg than I would have text next to the video that says 23mb. I tried looking through all the android mp.get functions but could not find what Iam looking for. Please help. Maybe I missed a function in Android. Or mayber there is anouther way to accomplush this. Thanks.
The value of the HTTP content-length header will provide the size of the file being downloaded.
Take a look at URLConnection.getHeaderField(String key) or HttpMessage.getFirstHeader(String name) depending on your server access code.
Try this will work in case the http server is giving the file size
URL myUrl = new URL("http://your_url.com/file.mp3");
URLConnection urlConnection = myUrl.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();
file_size = file_size /1024;
Or second Version try this.
URL myUrl = new URL("http://your_url.com/file.mp3");
myConnection = myUrl.openConnection();
List headersize = myConnection.getHeaderFields().get("content-Lenght");
Related
Has anyone released code to show the full HTTP request/response headers, any intermediate redirects, and any cookie data for the Android HttpURLConnection? This would be similar to Firefox Web Console
I roughly know how to write this myself, but 1) it's a non-trivial amount of code 2) it's tricky to get this kind of code to work in all instances. So i'm interested in finding a readymade solution. I know how to tcpdump the emulator, but I'm searching for code to print this information into the Android Log class for really quick runtime debugging.
for header fields
URL url = new URL(str_url);
HttpURLConnection conection = (HttpURLConnection) url.openConnection();
conection.setConnectTimeout(TIMEOUT_SOCKET);
conection.setReadTimeout(TIMEOUT_CONNECTION);
conection.addRequestProperty("Accept-Encoding", "gzip");
RedirectLocations locations = new RedirectLocations();
// here u get all header fields and properties write it in logs
conection.getHeaderFields();
conection.getRequestProperties();
// conection.getOutputStream().write(buffer);
// download the file
InputStream is = conection.getInputStream();
// This is file path were a; quiz data will get saved.
// String file_path = context.getDir(folder,Activity.MODE_PRIVATE).getAbsolutePath();
return unzip(is,save_file_path);
for redirects
link
after u get response, again u ve to look for header fields
I'm Unable to download file from FTP server using URLConnection having spaces in pat
String s = "ftp://username:password#ftpclient:21/AAB BBC/hhhh 0001.jpg";
URL u = new URL(s);
URLConnection uc = u.openConnection();
BufferedOutputStream bos = new BufferedOutputStream(uc.getOutputStream());
Dont want FTP client solution.
Using URLencoder getting 550 error file not found.
Thanks,
Gaurav
Are you using Apache commons Library ? If so , use this code
try {
FileOutputStream desFileStream = new FileOutputStream(desFilePath);;
status = FTP_object.retrieveFile(srcFilePath, desFileStream);
desFileStream.close();
return status;
} catch (Exception e) {
Log.d(TAG, "download failed");
}
You shouldn't have spaces in your URL itself per RFC1738 section 2.2 Run it through the URL Encode method to encode it:
String s = "ftp://username:password#ftpclient:21/AAB BBC/hhhh 0001.jpg";
String encodedUrl = URLEncoder.encode(s,"UTF-8");
I realize you said you were already doing the encoding, but that returned a 550 error. I didn't see the encoding so am just mentioning it should be needed.
I would really try this from a browser and see if you can get to it. I would also dump out the URL it is using and try that from a browser (or wget, curl, whatever you have handy). The 550 is listed as being a "permission" problem, not file not found, so I'm a little surprised at that, but that may be the short code and come up as an error to prevent people from poking around testing user/password combinations. Hard to say.
The other question I have for you is that you mention you don't want a client solution, but you seem to be writing a client not a server. You're going to port 21, which is the default FTP port for a server.
I would try various combinations of the encoding and see if maybe you're not encoding everything...you should encode the url path. Does the password have any funky characters in it?
Testing from the browser directly will give you a lot of insight.
I am trying to develop an application where I can get the HTML source of any web page. I am able to get the code but when I am trying the same code using some facebook profile links, it gives me an empty string.
I am using HttpURLConnection. I am trying the code as follows:
URL url = new URL(urlPassed);
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
BufferedInputStream buffer = new BufferedInputStream(urlc.getInputStream());
and I am trying to read that buffer in a loop as buffer.read(by) where "by" is a byte array of size 7024. However, it works fine for all other web pages except facebook pages.
Any reason for this? Any idea to solve this?
I'm downloading a file in my android app and I want to display a progressbar. I need to know the total file size by using this method : here.
The problem is that I always get -1 despite the fact that it's set on the server and I can see it from my computer.
This is the code :
URL url = new URL(fileUrl);
URLConnection ucon = url.openConnection();
ucon.connect();
int length = ucon.getContentLength();
What is the problem ?
This is not related to Android, most probably it was not sent from the server..please use any sniffing tool to make sure the content length header is there.
Can anybody tell me how to get the size of a file before downloading it from server(may be http,ftp or anything) in android?.Does streaming works?. Please Help me..
Regards
Varnesh
Try this will work in case the http server is giving the file size
URL myUrl = new URL("http://jamants.com/file.mp3");
URLConnection urlConnection = myUrl.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();
Or second Version try this.
URL myUrl = new URL("http://jamants.com/file.mp3");
myConnection = myUrl.openConnection();
List headersize = myConnection.getHeaderFields().get("content-Lenght");
It Works in my project.
If you are using the built-in Apahce HTTP library to make a request for a file, you can get the file size by requesting the headers only. Inside the headers will be a "content-length" attribute that will indicate the number of bytes of the requested file. Requesting the headers will not request the file itself.