Check if the SDCard is present, boolean is always true

前端 未结 7 1998
故里飘歌
故里飘歌 2020-11-29 05:42

In my splash screen, I want to check if the phone has a SDCard. The Boolean statement is beneath:

    Boolean isSDPresent = android.os.Environment.getExterna         


        
7条回答
  •  独厮守ぢ
    2020-11-29 05:49

    This code snippet might be of use for you to detect and deal with the exception of Samsung devices:

    public static String getExternalStorage() {
    
        String str = Environment.getExternalStorageDirectory().getAbsolutePath();
        if ( isSamsungDevice() ) {
            if ( isSamsungExternalSDMounted() ) {
                str += "/external_sd";
            }
        }
        return str;
    }
    
    private static boolean isSamsungDevice() {
    
        return new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/external_sd" ).exists();
    }
    
    private static boolean isSamsungExternalSDMounted() {
    
        return exec( "mount" ).indexOf( "external_sd" ) >= 0;
    }
    
    public static String exec( String paramString ) {
    
        try {
            String str = "";
            InputStream localInputStream = Runtime.getRuntime().exec( paramString ).getInputStream();
            byte[] arrayOfByte = new byte[1024];
            StringBuilder localStringBuilder = new StringBuilder();
            while ( true ) {
    
                int i = localInputStream.read( arrayOfByte, 0, arrayOfByte.length );
                if ( i == -1 ) {
                    str = localStringBuilder.toString();
                    break;
                }
    
                localStringBuilder.append( new String( arrayOfByte, 0, i ) );
            }
    
            return str;
        } catch ( IOException e ) {
            e.printStackTrace();
            return null;
        }
    }
    

提交回复
热议问题