Android很多手机现在都有EMMC存储(一般是2G),一些手机并没有挂载在getExternalStorageDirectory()这个节点上(该节点用于挂载外部sdcard了).而是挂载到"/mnt/emmc"节点上,另外一些手机把

EMMC存储直接 挂载到了 getExternalStorageDirectory()

  这个节点上,而对于真正的外部sdcard挂载到了 "/mnt/sdcard2" 这个节点上。

getExternalStorageDirectory()

   这个节点上,其次是 "/mnt/sdcard2" 这个节点上,再次是 "/mnt/emmc",最后才是手机的内部存储(即“/data”区域)。

  另外,在很多手机上,虽然我们使用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 total 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
   ;
  
}
  
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
    
   static
    
   String
    getFileName
   (
   String
    file
   )
    
   {
  
    
   if
    
   (
   file 
   ==
    
   null
   )
  
        
   return
    
   null
   ;
  
    
   int
    index 
   =
    file
   .
   lastIndexOf
   (
   "/"
   );
  
    
   return
    file
   .
   substring
   (
   index 
   +
    
   1
   );
  
}
  
public
    
   static
    
   boolean
    runShellScriptForWait
   (
   final
    
   String
    cmd
   )
    
   throws
    
   SecurityException
    
   {
  
    
   ShellThread
    thread 
   =
    
   new
    
   ShellThread
   (
   cmd
   );
  
    thread
   .
   setDaemon
   (
   true
   );
  
    thread
   .
   start
   ();
  
    
   int
    k 
   =
    
   0
   ;
  
    
   while
    
   (!
   thread
   .
   isReturn
   ()
    
   &&
    k
   ++
    
   <
    
   20
   )
    
   {
  
        
   try
    
   {
  
            
   Thread
   .
   sleep
   (
   50
   );
  
        
   }
    
   catch
    
   (
   InterruptedException
    e
   )
    
   {
  
            e
   .
   printStackTrace
   ();
  
        
   }
  
    
   }
  
    
   if
    
   (
   k 
   >=
    
   20
   )
    
   {
  
        thread
   .
   interrupt
   ();
  
    
   }
  
    
   return
    thread
   .
   isSuccess
   ();
  
}
  
/** 用于执行shell脚本的线程 */
  
private
    
   static
    
   class
    
   ShellThread
    
   extends
    
   Thread
    
   {
  
private
    
   boolean
    isReturn
   ;
  
private
    
   boolean
    isSuccess
   ;
  
private
    
   String
    cmd
   ;
  


  
  
public
    
   boolean
    isReturn
   ()
    
   {
  
    
   return
    isReturn
   ;
  
}
  


  
  
public
    
   boolean
    isSuccess
   ()
    
   {
  
    
   return
    isSuccess
   ;
  
}
  


  
  
/** @param cmd shell命令内容
  
 * @param isReturn 线程是否已经返回
  
 * @param isSuccess Process是否执行成功 */
  
public
    
   ShellThread
   (
   String
    cmd
   )
    
   {
  
    
   this
   .
   cmd 
   =
    cmd
   ;
  
}
  


  
  
@Override
  
public
    
   void
    run
   ()
    
   {
  
    
   try
    
   {
  
        
   Runtime
    runtime 
   =
    
   Runtime
   .
   getRuntime
   ();
  
        
   Process
    proc
   ;
  
        
   try
    
   {
  
            proc 
   =
    runtime
   .
   exec
   (
   cmd
   );
  
            isSuccess
   =(
   proc
   .
   waitFor
   ()==
   0
   );
  
        
   }
    
   catch
    
   (
   IOException
    e
   )
    
   {
  
            
   // TODO Auto-generated catch block
  
            e.printStackTrace();
  
        }
  
        isSuccess = true;
  
    } catch (InterruptedException e) {
  
    }
  
    isReturn = true;
  
}
  
}
  


  
  
}