kga*_*oid 6 android uri file filesize android-gallery
我正在从图库中选择图像,我想以编程方式确定图像的大小(kb或mb)。这是我写的:
public String calculateFileSize(Uri filepath)
{
//String filepathstr=filepath.toString();
File file = new File(filepath.getPath());
// Get length of file in bytes
long fileSizeInBytes = file.length();
// Convert the bytes to Kilobytes (1 KB = 1024 Bytes)
long fileSizeInKB = fileSizeInBytes / 1024;
// Convert the KB to MegaBytes (1 MB = 1024 KBytes)
long fileSizeInMB = fileSizeInKB / 1024;
String calString=Long.toString(fileSizeInMB);
return calString;
}
Run Code Online (Sandbox Code Playgroud)
从图库中选择图像的uri来的很完美。但是值fileSizeInBytes是零。从图库中onActivityResult选择图像后,我在调用此方法。之前我在这里看到了几个相同的问题。但是没有一个对我有用。有什么办法吗?
改变
public String calculateFileSize(Uri filepath)
{
//String filepathstr=filepath.toString();
File file = new File(filepath.getPath());
long fileSizeInKB = fileSizeInBytes / 1024;
// Convert the KB to MegaBytes (1 MB = 1024 KBytes)
long fileSizeInMB = fileSizeInKB / 1024;
String calString=Long.toString(fileSizeInMB);
Run Code Online (Sandbox Code Playgroud)
到
public String calculateFileSize(String filepath)
{
//String filepathstr=filepath.toString();
File file = new File(filepath);
float fileSizeInKB = fileSizeInBytes / 1024;
// Convert the KB to MegaBytes (1 MB = 1024 KBytes)
float fileSizeInMB = fileSizeInKB / 1024;
String calString=Float.toString(fileSizeInMB);
Run Code Online (Sandbox Code Playgroud)
当你使用它时long,它会截断后面的所有数字.,所以如果你的大小小于 1MB,你将得到 0。
所以改为float使用long
| 归档时间: |
|
| 查看次数: |
9770 次 |
| 最近记录: |