Android免费/可用空间始终返回0字节
我正在使用context.getFilesDir()以防无法将文件存储在外部存储器中。然后我使用getAvailableBytes()或bytesAvailable=(long)stat.getBlockSize()*(long)stat.getAvailableBlocks();基于构建来检查可用空间 在emulator和我的测试设备上,它运行良好。但当其他人从play安装我的应用程序时,他们总是获得0字节的可用空间。有人能给出一个提示或解决方案为什么会发生这种情况吗 我应该使用Environment.getDataDirectory().getAbsolutePath()而不是context.getFileDir().getAbsolutePath(),这是原因吗?。但它在emulator或我的开发设备中是如何工作的呢 请查找以下代码:Android免费/可用空间始终返回0字节,android,Android,我正在使用context.getFilesDir()以防无法将文件存储在外部存储器中。然后我使用getAvailableBytes()或bytesAvailable=(long)stat.getBlockSize()*(long)stat.getAvailableBlocks();基于构建来检查可用空间 在emulator和我的测试设备上,它运行良好。但当其他人从play安装我的应用程序时,他们总是获得0字节的可用空间。有人能给出一个提示或解决方案为什么会发生这种情况吗 我应该使用Environ
public static String getOutputDirectory(Context context)
{
String outputstore = System.getenv("SECONDARY_STORAGE");
if(outputstore == null)
{
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
outputstore = Environment.getExternalStorageDirectory().getAbsolutePath();
else
outputstore = context.getFilesDir().getAbsolutePath();
}
return outputstore;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static boolean isFreeSpace(Context context)
{
long bytesAvailable =0;
StatFs stat = new StatFs(getOutputDirectory(context));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
{
bytesAvailable = stat.getAvailableBytes();
}
else
{
bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
}
long megAvailable = bytesAvailable / (1024L * 1024L);
if(megAvailable <= Constants.SPACE_CHECK_MEGABYTES)
{
return false;
}
else
{
return true;
}
}
}
公共静态字符串getOutputDirectory(上下文)
{
字符串outputstore=System.getenv(“辅助存储”);
if(outputstore==null)
{
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
outputstore=Environment.getExternalStorageDirectory().getAbsolutePath();
其他的
outputstore=context.getFilesDir().getAbsolutePath();
}
返回输出存储;
}
@TargetApi(Build.VERSION\u code.JELLY\u BEAN\u MR2)
公共静态布尔值isFreeSpace(上下文)
{
长字节可用=0;
StatFs stat=newstatfs(getOutputDirectory(context));
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.JELLY\u BEAN\u MR2)
{
bytesAvailable=stat.getAvailableBytes();
}
其他的
{
bytesAvailable=(长)stat.getBlockSize()*(长)stat.getAvailableBlocks();
}
长兆可用=字节可用/(1024L*1024L);
如果(megAvailable您可以使用它,如果它能达到目的,但是它使用了一些不推荐的方法
public static long getAvailableStorage() {
String storageDirectory = null;
storageDirectory = Environment.getExternalStorageDirectory().toString();
try {
StatFs stat = new StatFs(storageDirectory);
long avaliableSize = ((long) stat.getAvailableBlocks() * (long) stat
.getBlockSize());
return avaliableSize;
} catch (RuntimeException ex) {
return 0;
}
}
问题在于字符串outputstore=System.getenv(“SECONDARY_STORAGE”);在三星和其他一些设备中,虽然没有安装sd卡,但此语句返回值而不是null。下面的代码将修复此问题
String outputstore = System.getenv("SECONDARY_STORAGE");
if(outputstore != null)
{
File secondaryStorage = new File(outputstore + File.separator + Constants.VIDEO_PREFIX);
if(!(secondaryStorage.exists() && secondaryStorage.canWrite()))
{
//If sd card found(or a false positive) and not writable or exists
//then try getting internal storages
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
outputstore = Environment.getExternalStorageDirectory().getAbsolutePath();
else
outputstore = context.getFilesDir().getAbsolutePath();
}
}
else
{
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
outputstore = Environment.getExternalStorageDirectory().getAbsolutePath();
else
outputstore = context.getFilesDir().getAbsolutePath();
}
谢谢回复。但是如果外部存储没有挂载,那么我需要写入内部存储。我之前尝试过不推荐的方法,但这些方法也不起作用。上述方法也适用于内部存储,只需检查存储是否挂载。我有小米MI3,它可以很好地工作,就像小米MI3只有内部存储但如果未装入,则需要使用context.getFilesDir().getAbsolutePath(),不是吗?这与Environment.getExternalStorageDirectory()不同。我相信不需要单独的API调用。