Get the size of an Android file resource?

折月煮酒 提交于 2019-11-27 06:43:58

问题


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

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