我正在开发Android视频应用程序,我正在使用Camera Intent录制视频并从图库中获取视频。录制视频或从图库中选择视频后,我想知道我拍摄的视频的大小。如果选择的视频大小超过5MB,我想制作一个向用户显示Toast消息的逻辑。 我做了一个不起作用的底层逻辑,给了我0值,我试图从URI中获取大小。
提前致谢。
我的逻辑无效
java.net.URI juri = new java.net.URI(uri.toString());
File mediaFile = new File(juri.getPath());
long fileSizeInBytes = mediaFile.length();
long fileSizeInKB = fileSizeInBytes / 1024;
long fileSizeInMB = fileSizeInKB / 1024;
if (fileSizeInMB > 5) {
Toast.makeText(this,"Video files lesser than 5MB are allowed",Toast.LENGTH_LONG).show();
return;
}
这是我用来从Gallery获取视频和录制视频的代码。
public void takeVideoFromCamera(){
File mediaFile =new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/myvideo.mp4");
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Uri videoUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// videoUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", mediaFile);
videoUri = FileProvider.getUriForFile(this, "i.am.ce.by.ncy.provider", mediaFile);
} else {
videoUri = Uri.fromFile(mediaFile);
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 5491520L);//5*1048*1048=5MB
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,45);
startActivityForResult(intent, VIDEO_CAPTURE);
}
public void takeVideoFromGallery(){
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
long maxVideoSize = 5 * 1024 * 1024; // 10 MB
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, maxVideoSize);
startActivityForResult(Intent.createChooser(intent,"Select Video"),REQUEST_TAKE_GALLERY_VIDEO);
}
onActivityResult代码
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_OK) {
switch (requestCode) {
case VIDEO_CAPTURE:
if (resultCode == RESULT_OK) {
showVideoImage(data.getData());
// Here I want to know what is the size of my Video File
}
break;
case REQUEST_TAKE_GALLERY_VIDEO:
if (resultCode == RESULT_OK) {
showVideoGallery(data);
// Here I want to know what is the size of my Video File
}
break;
}
答案 0 :(得分:3)
AssetFileDescriptor fileDescriptor = getApplicationContext().getContentResolver().openAssetFileDescriptor(uri , "r");
long fileSize = fileDescriptor.getLength();
答案 1 :(得分:3)
AssetFileDescriptor.length
ParcelFileDescriptor.getStatSize
内隐式检查 AssetFileDescriptor.length
MediaStore/ContentResolver
是否 URI 具有 content://
方案应该适用于 file://
、content://
方案。如果找不到,则返回 -1L
:
fun Uri.length(contentResolver: ContentResolver)
: Long {
val assetFileDescriptor = try {
contentResolver.openAssetFileDescriptor(this, "r")
} catch (e: FileNotFoundException) {
null
}
// uses ParcelFileDescriptor#getStatSize underneath if failed
val length = assetFileDescriptor?.use { it.length } ?: -1L
if (length != -1L) {
return length
}
// if "content://" uri scheme, try contentResolver table
if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
return contentResolver.query(this, arrayOf(OpenableColumns.SIZE), null, null, null)
?.use { cursor ->
// maybe shouldn't trust ContentResolver for size: https://stackoverflow.com/questions/48302972/content-resolver-returns-wrong-size
val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
if (sizeIndex == -1) {
return@use -1L
}
cursor.moveToFirst()
return try {
cursor.getLong(sizeIndex)
} catch (_: Throwable) {
-1L
}
} ?: -1L
} else {
return -1L
}
}
答案 2 :(得分:1)
chain.doFilter(httpRequest, httpResponse);
java.net.URI juri = new java.net.URI(uri.toString());
File mediaFile = new File(juri.getPath());
不是Uri
。
File
ACTION_VIDEO_CAPTURE
does not return a Uri
此外,您已经知道文件的位置。您在showVideoImage(data.getData());
中创建了一个File
对象,并将其用于takeVideoFromCamera()
。保留该EXTRA_OUTPUT
对象(并将其保存在已保存的实例状态File
中),然后使用它来查找生成的视频的大小。
答案 3 :(得分:1)
private long getFileSize(Uri fileUri) {
Cursor returnCursor = getContentResolver().
query(fileUri, null, null, null, null);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
return returnCursor.getLong(sizeIndex);
}
检查here了解更多详情
答案 4 :(得分:0)
试试这个
文件文件=新文件(URI);
if (file.exists()){
long size = file.length();
}
当
时,值为= 0文件存在但包含零字节。 该文件不存在,或 该文件是一些特定于操作系统的特殊文件。