Get the size of an Android file resource?

孤人 提交于 2019-11-29 09:12:56

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

Try this lines:

InputStream ins = context.getResources().openRawResource (R.raw.video)
int videoSize = ins.available();

Try this:

AssetFileDescriptor sampleFD = getResources().openRawResourceFd(R.raw.video);
long size = sampleFD.getLength()

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!