Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file.length return always 0 in android

Tags:

file

android

I want to check size of file where I have only URI content://media/external/video/media/813 to get size of file I use:

File file=new File(uri.getPath());
                    long ls = file.length();

and always get 0. Any ideas?

like image 365
edi233 Avatar asked Nov 29 '25 11:11

edi233


2 Answers

Try below code to get length of your file

 Cursor returnCursor =
                getContentResolver().query(uri, null, null, null, null);
        int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
        returnCursor.moveToFirst();
        int size = (int) returnCursor.getLong(sizeIndex);
like image 113
Nikhil Sharma Avatar answered Dec 01 '25 01:12

Nikhil Sharma


try this,

String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();

File file = new File(filePath);
long ls = file.length();
like image 44
shinilms Avatar answered Dec 01 '25 01:12

shinilms



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!