现在的位置: 首页 > 综合 > 正文

Android中一些数据存储函数的封装

2013年07月04日 ⁄ 综合 ⁄ 共 6533字 ⁄ 字号 评论关闭

本人博客原文

Android很多手机现在都有EMMC存储(一般是2G),一些手机并没有挂载在getExternalStorageDirectory()这个节点上(该节点用于挂载外部sdcard了).而是挂载到"/mnt/emmc"节点上,另外一些手机把EMMC存储直接挂载到了getExternalStorageDirectory() 这个节点上,而对于真正的外部sdcard挂载到了"/mnt/sdcard2"这个节点上。另外/system/etc/vold.fstab文件中描述了SD
card的挂载点,我们可以读取它来得到关于
SD card的挂载点信息
因此当我们存储一个文件时首先应该存在 getExternalStorageDirectory()  这个节点上,其次是"/mnt/sdcard2"这个节点上,再次是"/mnt/emmc",再次考虑/system/etc/vold.fstab文件中的挂载点,最后才是手机的内部存储(即“/data”区域)。
注意:对于/system/etc/vold.fstab文件中的挂载点,很可能就是getExternalStorageDirectory()  这个节点上,或是"/mnt/sdcard2"这个节点上,或是是"/mnt/emmc"。但是其实做两次检查应该没有什么副作用,由于对于读取/system/etc/vold.fstab文件来得到描述SD
card的挂载点这个事情似乎不是一个官方的做法;我就把它放在了更低的优先级
  另外,在很多手机上,虽然我们使用Context的openFileOutput(FILENAME, Context.MODE_WORLD_READABLE)的方式来创建文件,而且使用ls
-l看到该文件对别的应用程序来说其实已经有读的权限,但是别的应用程序实际上还是无法读取这些。这时我们需要在创建该文件的应用程序中对
getFilesDir()目录执行"chmod
705"
的操作来解决该问题。
我特把这些操作进行封装,以便使用。

package cn.edu.cdut.robin;
import java.io.File;
import java.io.IOException;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;
public class AppUtil {
static Context context;

static void init(Context cxt)
{
context = cxt;
}

/** get external Storage available space */
public static long getExternaltStorageAvailableSpace()
{
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED))
{
return 0;
}
File path = Environment.getExternalStorageDirectory();
StatFs statfs = new StatFs(path.getPath());
long blockSize = statfs.getBlockSize();
long availableBlocks = statfs.getAvailableBlocks();
return blockSize * availableBlocks;
}

/** get external Storage available space */
public static long getExternaltStorageTotalSpace()
{
File path = Environment.getExternalStorageDirectory();
StatFs statfs = new StatFs(path.getPath());
long blockSize = statfs.getBlockSize();
long totalBlocks = statfs.getBlockCount();
return blockSize * totalBlocks;
}

/** get sdcard2 external Storage available space */
public static long getSdcard2StorageAvailableSpace()
{
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED))
{
return 0;
}
String path = getSdcard2StorageDirectory();
File file = new File(path);
if (!file.exists())
return 0;
StatFs statfs = new StatFs(path);
long blockSize = statfs.getBlockSize();
long availableBlocks = statfs.getAvailableBlocks();
return blockSize * availableBlocks;
}

/** get sdcard2 external Storage total space */
public static long getSdcard2StorageTotalSpace()
{
String path = getSdcard2StorageDirectory();
File file = new File(path);
if (!file.exists())
return 0;
StatFs statfs = new StatFs(path);
long blockSize = statfs.getBlockSize();
long totalBlocks = statfs.getAvailableBlocks();
return blockSize * totalBlocks;
}

/** get EMMC internal Storage available space */
public static long getEmmcStorageAvailableSpace()
{
String path = getEmmcStorageDirectory();
File file = new File(path);
if (!file.exists())
return 0;
StatFs statfs = new StatFs(path);
long blockSize = statfs.getBlockSize();
long availableBlocks = statfs.getAvailableBlocks();

return blockSize * availableBlocks;
}

/** get EMMC internal Storage available space */
public static long getEmmcStorageTotalSpace()
{
String path = getEmmcStorageDirectory();
File file = new File(path);
if (!file.exists())
return 0;
StatFs statfs = new StatFs(path);
long blockSize = statfs.getBlockSize();
long totalBlocks = statfs.getBlockCount();

return blockSize * totalBlocks;
}
static FstabReader fsReader =null;
/** get other external Storage available space*/
public static long getOtherExternaltStorageAvailableSpace() {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return 0;
}
if(otherExternalStorageState==kOtherExternalStorageStateUnable)
return 0;
if(otherExternalStorageDirectory==null)
{
getOtherExternalStorageDirectory();
}
if(otherExternalStorageDirectory==null)
return 0;
StatFs statfs = new StatFs(otherExternalStorageDirectory);
long blockSize = statfs.getBlockSize();
long availableBlocks = statfs.getAvailableBlocks();
return blockSize * availableBlocks;
}
private static String otherExternalStorageDirectory=null;
private static int kOtherExternalStorageStateUnknow=-1;
private static int kOtherExternalStorageStateUnable=0;
private static int kOtherExternalStorageStateIdle=1;
private static int otherExternalStorageState=kOtherExternalStorageStateUnknow;
public static String getOtherExternalStorageDirectory()
{
if(otherExternalStorageState==kOtherExternalStorageStateUnable)
return null;
if(otherExternalStorageState==kOtherExternalStorageStateUnknow)
{
FstabReader fsReader = new FstabReader();
if(fsReader.size()<=0)
{
otherExternalStorageState=kOtherExternalStorageStateUnable;
return null;
}
List<StorageInfo> storages = fsReader.getStorages();
/*对于可用空间小于100M的挂载节点忽略掉*/
long availableSpace=100<<(20);
String path=null;
for(int i=0;i<storages.size();i++)
{
StorageInfo info=storages.get(i);
if(info.getAvailableSpace()>availableSpace){
availableSpace=info.getAvailableSpace();
path=info.getPath();
}
}
otherExternalStorageDirectory=path;
if(otherExternalStorageDirectory!=null){
otherExternalStorageState=kOtherExternalStorageStateIdle;
}
else {
otherExternalStorageState=kOtherExternalStorageStateUnable;
}
}
return otherExternalStorageDirectory;
}
public static long getInternalStorageAvailableSpace()
{
String path = getInternalStorageDirectory();
StatFs stat = new StatFs(path);
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return blockSize * availableBlocks;
}

/**
* 获取手机内部总的存储空间
*
* @return
*/
public static long getInternalStorageTotalSpace()
{
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return totalBlocks * blockSize;
}

public final static String getExternalStorageDirectory()
{
return Environment.getExternalStorageDirectory() + File.separator + "";
}

public final static String getExternalStoragePublicDirectory(String type)
{
return Environment.getExternalStoragePublicDirectory(type)
.getAbsolutePath();
}

public final static String getSdcard2StorageDirectory()
{
return "/mnt/sdcard2";
}

public final static String getEmmcStorageDirectory()
{
return "/mnt/emmc";
}

private static String externalStoragePrivateDirectory;

static String getExternalPrivateFilesDirectory()
{
if (externalStoragePrivateDirectory == null)
externalStoragePrivateDirectory = context.getExternalFilesDir(null)
.getAbsolutePath();
return externalStoragePrivateDirectory;
}

private static String internalStorageDirectory;

public final static String getInternalStorageDirectory()
{
if (TextUtils.isEmpty(internalStorageDirectory))
{
File file = context.getFilesDir();
internalStorageDirectory = file.getAbsolutePath();
if (!file.exists())
file.mkdirs();
String shellScript = "chmod 705 " + internalStorageDirectory;
runShellScriptForWait(shellScript);
}
return internalStorageDirectory;
}

public static boolean isInternalStoragePath(String path)
{
String rootPath = getInternalStorageDirectory();
if (path != null && path.startsWith(rootPath))
return true;
return false;
}

public

抱歉!评论已关闭.