安卓:获取资源文件的大小?大小、文件、资源

由网友(浅雪)分享简介:我在我的资源在原始文件夹中的视频文件。我想找到的文件的大小。我有这块code: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:...

我在我的资源在原始文件夹中的视频文件。我想找到的文件的大小。 我有这块code:

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());

但它总是给我的大小为0。我究竟做错了什么?

But it always gives me that the size is 0. What am I doing wrong?

推荐答案

您无法使用文件资源。使用资源 AssetManager 来获得的InputStream 的资源,然后调用就可以了用()方法。

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.

这样的:

InputStream is = context.getResources().openRawResource(R.raw.nameOfFile);
int sizeOfInputStram = is.available(); // Get the size of the stream
阅读全文

相关推荐

最新文章