我正在尝试从外部SD卡计算总内存大小(视频和图像)。例如,有一个方法MediaStore.Audio.Media.getContentUriForPath(String path)
。从那里我可以获取URI并计算音频文件的大小。通过..
private long getMedia(Uri uri) {
Log.e("URI",uri+"");
String[] proj = { MediaStore.MediaColumns.SIZE };
Cursor cursor = ((Activity) context).getContentResolver().query(uri, proj, null, null, null);
long size = DirectoryMediaSize.getMediaSize(cursor);
cursor.close();
return size;
}
没有像MediaStore.Video.Media.getContentUriForPath()
这样的方法。那么如何计算(视频和图像)的大小???
答案 0 :(得分:0)
尝试以下代码: -
Uri selectedImageURI = data.getData();
File imageFile = new File(getRealPathFromURI(selectedImageURI));
long check = ((imageFile.length() / 1024));
if (check < 5000)
{
// less then 5 mb
}
else
{
// greater then 5 mb
}
功能getRealPathFromURI
String getRealPathFromURI(Uri contentURI)
{
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null)
{ // Source is Dropbox or other similar local file path
result = contentURI.getPath();
}
else
{
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}