admin
2024-09-13 23cb5200b7a8fb57af5e8006612181bae9c0c95d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.weikou.beibeivideo.util;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.os.StatFs;
 
import com.weikou.beibeivideo.entity.SDCardEntity;
 
public class SDCardUtil {
 
    public static long getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return availableBlocks * blockSize;
    }
 
    public static long getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return totalBlocks * blockSize;
    }
 
    public static boolean externalMemoryAvailable() {
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }
 
    public static long getAvailableExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            return availableBlocks * blockSize;
        } else {
            return -1;
        }
    }
 
    public static long getTotalExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            return totalBlocks * blockSize;
        } else {
            return -1;
        }
    }
 
    public static List getExtSDCardPath() {
        List lResult = new ArrayList();
        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("mount");
            InputStream is = proc.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {
                if (line.contains("extSdCard")) {
                    String[] arr = line.split(" ");
                    String path = arr[1];
                    File file = new File(path);
                    if (file.isDirectory()) {
                        lResult.add(path);
                    }
                }
            }
            isr.close();
        } catch (Exception e) {
        }
        return lResult;
    }
 
    public static String getSotrageSize(long size) {
 
        if (size >= 1024) {
            float s = ((float) size / 1024);
            return (new java.text.DecimalFormat("#.00").format(s)) + "GB";
        } else {
            return size + "MB";
        }
    }
 
    @SuppressWarnings("deprecation")
    private static SDCardEntity getSDCardEntity(String path) {
        SDCardEntity se = new SDCardEntity();
        se.setPath(path);
        try {
            File ff = new File(path);
            if (!ff.exists())
                ff.mkdirs();
            StatFs stat = new StatFs(path);
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            long totalBlocks = stat.getBlockCount();
            se.setAvailableSize(availableBlocks * blockSize);
            se.setTotalSize(totalBlocks * blockSize);
        } catch (Exception e) {
            File f = new File(path);
            if (f.exists()) {
                se.setAvailableSize(f.getFreeSpace());
                se.setTotalSize(f.getTotalSpace());
            }
        }
        return se;
    }
 
    /**
     * 获取外部存储卡
     *
     * @param context
     * @return
     */
    public static SDCardEntity getSDCardPath(Context context) {
        SDCardEntity entity = new SDCardEntity();
        // if (PackageUtils2.getAndroidOSVersion() > 9) {
        StorageList list = new StorageList(context);
        String[] sts = list.getVolumePaths();
        if (sts == null || sts.length == 0)
            return null;
        else {
            if (sts[0].equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
                entity = getSDCardEntity(sts[0]);
                if (entity.getTotalSize() > 0)
                    return entity;
            } else if (sts.length > 1) {
                entity.setPath(sts[1]);
                entity = getSDCardEntity(sts[1]);
                if (entity.getTotalSize() > 0)
                    return entity;
            } else
                return null;
        }
        /*
         * } else { if (Environment.getStorageState(Environment.)
         * .equals(Environment.MEDIA_MOUNTED)) { // 为true的话,外置sd卡存在 } }
         */
 
        return null;
    }
 
    public static void setDeaultStorage(Context context, int type) {
        SharedPreferences share = context.getSharedPreferences("storagestate",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = share.edit();
        editor.putInt("stro", type);
        editor.commit();
    }
 
    public static void initStorage(Context context) {
        SharedPreferences share = context.getSharedPreferences("storagestate",
                Context.MODE_PRIVATE);
        if (!share.getBoolean("StorageSetting", false))
            return;
        SDCardEntity entity = getSDCardPath(context);
        if (entity != null) {
            if (entity.getAvailableSize() > getAvailableExternalMemorySize()) {
                setDeaultStorage(context, STORAGE_SDCARD);
            }
        }
        SharedPreferences.Editor editor = share.edit();
        editor.putBoolean("StorageSetting", true);
        editor.commit();
 
    }
 
    public static int getDeaultStorage(Context context) {
        SharedPreferences share = context.getSharedPreferences("storagestate",
                Context.MODE_PRIVATE);
        return share.getInt("stro", 0);
    }
 
    public static String getDownLoadPath(Context context) {
        String name = context.getPackageName();
        int type = getDeaultStorage(context);
        if (type == STORAGE_MOBILE) {
            // 下载文件存放到根目录 Download文件夹下
            // File f = Environment
            // .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
 
            // 下载文件存放到app安装目录下
            File f = context.getExternalFilesDir("影视大全缓存文件");
            // File f = context.getExternalCacheDir();s
            if (f == null) {
                f = Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            }
            if (!f.exists())
                f.mkdirs();
            return f.getPath() + "/";
        } else if (type == STORAGE_SDCARD) {
            SDCardEntity entity = getSDCardPath(context);
            if (entity == null) {
                File f = Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                if (!f.exists())
                    f.mkdirs();
                return f.getPath() + "/";
            }
            String root = entity.getPath();
            if (!new File(root + File.separator + name).exists())
                new File(root + File.separator + name).mkdirs();
            if (!new File(root + File.separator + name + File.separator
                    + "video").exists())
                new File(root + File.separator + name + File.separator
                        + "video").mkdirs();
            return root + File.separator + name + File.separator + "video";
        }
        return "";
    }
 
    public static SDCardEntity getDownLoadEntity(Context context) {
 
        String path = getDownLoadPath(context);
        return getSDCardEntity(path);
    }
 
    public final static int STORAGE_SDCARD = 1;
    public final static int STORAGE_MOBILE = 0;
}