1

Hi I am new for android and in my app I have to find the file size before downloading it.

For this I wrote the code below, but it's giving me content length 2588 but the actual size of my file is 745 MB.

Please help me. How can I find the exact file size in MB's?

code:-

URL url = new URL("mu url");
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();
Elydasian
  • 2,016
  • 5
  • 23
  • 41
Krish
  • 4,166
  • 11
  • 58
  • 110
  • You will only be able to see value which has been set in content length header field. That value you would be able to access. – rohitanand Mar 31 '17 at 11:55
  • using above url i have to find file size in MB's if you know please help – Krish Mar 31 '17 at 11:57
  • please look this .it will help you [http://stackoverflow.com/questions/2983073/how-to-know-the-size-of-a-file-before-downloading-it](http://stackoverflow.com/questions/2983073/how-to-know-the-size-of-a-file-before-downloading-it) – Ghanshyam Sharma Mar 31 '17 at 11:58

2 Answers2

1

You're probably using the wrong URL/file to get the size of the file you're expecting:

URL url = new URL("http://adultswim-vodlive.cdn.turner.com/ce8bfc5c729b0587eed7cf08b952c8c4/master.m3u8");

M3U8 files are plain-text playlist files - in your case the file that your URL is pointing to is actually only 2588 bytes.

iVoid
  • 701
  • 3
  • 14
  • so how can i find size in MB's – Krish Mar 31 '17 at 12:02
  • can you please give full code because i am new for android – Krish Mar 31 '17 at 12:04
  • The 745 MB file size you're expecting - how do you know the file is 745 MB? What is the file type of that 745 MB file and what URL you're using to access the 745 MB file? – iVoid Mar 31 '17 at 12:06
  • Actually we are using One Download Manager SDk using abover M3U8 file when i download it's showing 745MB ,But before downloading i want to find that M3U8 file size exactly – Krish Mar 31 '17 at 12:10
  • i hope you can help me and you can give solution to my problem – Krish Mar 31 '17 at 12:12
  • Your download manager SDK is probably referencing the individual multimedia content present in the playlist and combining them into a single multimedia file and giving it. If your download manager SDK doesn't currently support a size() API, it's not easy to find out the size of the complete multimedia content pointed to by a playlist file like M3U8. – iVoid Mar 31 '17 at 12:20
  • Please check this answer - http://stackoverflow.com/questions/41799396/figure-out-the-file-size-of-a-m3u8-url-referenced-video - you could probably try something similar, but I don't know anything about this. – iVoid Mar 31 '17 at 12:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/139583/discussion-between-krish-and-ivoid). – Krish Mar 31 '17 at 12:23
-1

You can try this to find file size:

String value = null;     
long fileSize = getFileSize(file)/1024;  
if(fileSize >= 1024)
   value = fileSize/1024+" Mb";
else
   value = fileSize+" Kb";


// this method returns file size
public long getFileSize(File f) {
    long size = 0;
    if (f.isDirectory()) {
        for (File file : f.listFiles()) {    
            size += getFolderSize(file);
        }
    } else {
        size=f.length();
    }
    return size;
}
Amey Haldankar
  • 2,223
  • 1
  • 24
  • 22