SD卡工具类

  在集成sdk时有时无法确定能否读写sd,android6.0以后即使在manifest总申请了读写权限,若targetSdkVersion在23以下,默认的sd卡出于关闭状态则任然无法读写sd卡

package com.manny.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;
import java.io.FileOutputStream;

/**
* Created by manny on 2016/11/1.
*/

public class SdCardUtils
{
private final static String WRITE_EXTERNAL_STORAGE = “android.permission.WRITE_EXTERNAL_STORAGE”;

/**
 * 判断SdCard是否可用
 */
public static boolean isSdCardExist()
{
    return Environment.MEDIA_MOUNTED.equals( Environment.getExternalStorageState() );
}

/**
 * 获取SD卡路径
 */
public static String getSDCardPath()
{
    return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
}

/**
 * 获取SD卡的剩余容量 单位byte
 */
public static long getSDCardAllSize()
{
    if( isSdCardExist() )
    {
        StatFs stat = new StatFs( getSDCardPath() );
        // 获取空闲的数据块的数量
        long availableBlocks = ( long )stat.getAvailableBlocks() - 4;
        // 获取单个数据块的大小(byte)
        long freeBlocks = stat.getAvailableBlocks();
        return freeBlocks * availableBlocks;
    }
    return 0;
}

/**
 * 获取指定路径所在空间的剩余可用容量字节数,单位byte
 * @return 容量字节 SDCard可用空间,内部存储可用空间
 */
public static long getFreeBytes( String filePath )
{
    // 如果是sd卡的下的路径,则获取sd卡可用容量
    if( filePath.startsWith( getSDCardPath() ) )
    {
        filePath = getSDCardPath();
    }
    else
    {// 如果是内部存储的路径,则获取内存存储的可用容量
        filePath = Environment.getDataDirectory().getAbsolutePath();
    }
    StatFs stat = new StatFs( filePath );
    long availableBlocks = ( long )stat.getAvailableBlocks() - 4;
    return stat.getBlockSize() * availableBlocks;
}

/**
 * 获取系统存储路径
 */
public static String getRootDirectoryPath()
{
    return Environment.getRootDirectory().getAbsolutePath();
}

/**
 * 判断能否写入sd卡
 * @param context
 * @return
 */
public static boolean isCanWriteSD( Context context )
{
    try
    {
        if( isSdCardExist() )
        {
            boolean isPermissioneDclaration = checkSdcardPermission( context, WRITE_EXTERNAL_STORAGE );
            if( isPermissioneDclaration )
            {
                return writeFileToSdcard( context );
            }
        }
    }
    catch( Exception e )
    {
        LogUtils.e( e.toString() );
    }
    return false;
}

/**
 * 若已申明读写权限,则尝试能否写入文件到sd卡内
 */
public static boolean writeFileToSdcard( Context context )
{
    String destFilePath = Environment.getExternalStorageDirectory() + "/android/" + "test.txt";
    try
    {
        File file = new File( Environment.getExternalStorageDirectory() + "/android/" );
        if( !file.exists() )
        {
            file.mkdirs();
        }
        File file1 = new File( destFilePath );
        if( !file1.exists() )
        {
            file1.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream( file1 );
        String info = "This is a test";
        fos.write( info.getBytes() );
        fos.close();
        return true;
    }
    catch( Exception e )
    {
        e.printStackTrace();
    }
    finally
    {
        File fileCreate = new File( destFilePath );
        if( fileCreate.exists() )
        {
            new File( destFilePath ).delete();
        }
    }
    return false;
}

/**
 * 遍历manifes申请的所有权限
 */
public static boolean checkSdcardPermission(
        Context context,
        String checkPermission )
{
    try
    {
        PackageManager pm = context.getPackageManager();
        PackageInfo pacageInfo = pm.getPackageInfo( context.getPackageName(), PackageManager.GET_PERMISSIONS );
        String[] permissionStrings = pacageInfo.requestedPermissions;
        if( permissionStrings.length <= 0 )
        {
            return false;
        }
        for( String permission : permissionStrings )
        {
            if( checkPermission.equals( permission ) )
            {
                return true;
            }
        }
    }
    catch( PackageManager.NameNotFoundException e )
    {
        e.printStackTrace();
    }
    return false;
}

}

猜你喜欢

转载自blog.csdn.net/believeinbelieve/article/details/78922943