Ale*_*987 17 size resources android file
我的资源中的原始文件夹中有一个视频文件.我想找到文件的大小.我有这段代码:
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());
Run Code Online (Sandbox Code Playgroud)
但它总是让我的大小为0.我做错了什么?
Ale*_*dam 22
试试这一行:
InputStream ins = context.getResources().openRawResource (R.raw.video)
int videoSize = ins.available();
Run Code Online (Sandbox Code Playgroud)
she*_*hem 21
试试这个:
AssetFileDescriptor sampleFD = getResources().openRawResourceFd(R.raw.video);
long size = sampleFD.getLength()
Run Code Online (Sandbox Code Playgroud)
Ebo*_*ike 11
您不能File用于资源.使用Resources或AssetManager获取InputStream资源,然后调用该available()方法.
像这样:
InputStream is = context.getResources().openRawResource(R.raw.nameOfFile);
int sizeOfInputStram = is.available(); // Get the size of the stream
Run Code Online (Sandbox Code Playgroud)
AssetFileDescriptor afd = contentResolver.openAssetFileDescriptor(fileUri,"r");
long fileSize = afd.getLength();
afd.close();
Run Code Online (Sandbox Code Playgroud)
哪里fileUri是 Android 类型Uri