Akarsh M
Akarsh M

Reputation: 1611

How can I find size of any file from SD_Card?

I have a mp 3 file in SD card , I have path of that mp 3 / "any extension" file.

Now ,

How can I get the size of the mp 3 file using code ?

Upvotes: 0

Views: 76

Answers (3)

Pratik Dasa
Pratik Dasa

Reputation: 7439

Try below code:

File file = new File(PATH OF YOUR FILE);
long fileSize = file.length();

After this you can perform whatever action you want on size, means these data you will get in Bytes. You can convert it in KB, MB, GB and so on.

Upvotes: 1

Hariharan
Hariharan

Reputation: 24853

Try this..

File filenew = new File(selectedPath);
int file_size = Integer.parseInt(String.valueOf(filenew.length()/1024));

and also check out the example

http://www.mkyong.com/java/how-to-get-file-size-in-java/

Upvotes: 1

Amit Prajapati
Amit Prajapati

Reputation: 14335

Try this

        File file=new File(Environment.getExternalStorageDirectory(), "lala.mp3");

        if(file.exists()){

            double bytes = file.length();
            double kilobytes = (bytes / 1024);
            double megabytes = (kilobytes / 1024);
            double gigabytes = (megabytes / 1024);
            double terabytes = (gigabytes / 1024);
            double petabytes = (terabytes / 1024);
            double exabytes = (petabytes / 1024);
            double zettabytes = (exabytes / 1024);
            double yottabytes = (zettabytes / 1024);

            System.out.println("bytes : " + bytes);
            System.out.println("kilobytes : " + kilobytes);
            System.out.println("megabytes : " + megabytes);
            System.out.println("gigabytes : " + gigabytes);
            System.out.println("terabytes : " + terabytes);
            System.out.println("petabytes : " + petabytes);
            System.out.println("exabytes : " + exabytes);
            System.out.println("zettabytes : " + zettabytes);
            System.out.println("yottabytes : " + yottabytes);
        }else{
             System.out.println("File does not exists!");
        }

Upvotes: 0

Related Questions