问题
I have a video file in the raw folder in my resources. I want to find the size of the file. I have this piece of code:
Uri filePath = Uri.parse("android.resource://com.android.FileTransfer/" + R.raw.video);
File videoFile = new File(filePath.getPath());
Log.v("LOG", "FILE SIZE "+videoFile.length());
But it always gives me that the size is 0. What am I doing wrong?
回答1:
You can't use File
for resources. Use Resources
or AssetManager
to get an InputStream
to the resource, then call the available()
method on it.
Like this:
InputStream is = context.getResources().openRawResource(R.raw.nameOfFile);
int sizeOfInputStram = is.available(); // Get the size of the stream
回答2:
Try this lines:
InputStream ins = context.getResources().openRawResource (R.raw.video)
int videoSize = ins.available();
回答3:
Try this:
AssetFileDescriptor sampleFD = getResources().openRawResourceFd(R.raw.video);
long size = sampleFD.getLength()
回答4:
Slight variation to the answer by @shem
AssetFileDescriptor afd = contentResolver.openAssetFileDescriptor(fileUri,"r");
long fileSize = afd.getLength();
afd.close();
Where fileUri
is of type Android Uri
来源:https://stackoverflow.com/questions/6049926/get-the-size-of-an-android-file-resource