Storage.java revision 721d2d2a963799fad4bda9bb4b278c24fc469303
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.camera;
18
19import android.os.Environment;
20import android.os.StatFs;
21import android.util.Log;
22
23import java.io.File;
24
25class Storage {
26    private static final String TAG = "CameraStorage";
27
28    private static final String DCIM =
29            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString();
30
31    public static final String DIRECTORY = DCIM + "/Camera";
32
33    public static final long UNAVAILABLE = -1L;
34    public static final long PREPARING = -2L;
35    public static final long UNKNOWN_SIZE = -3L;
36
37    public static long getAvailableSpace() {
38        String state = Environment.getExternalStorageState();
39        if (Environment.MEDIA_CHECKING.equals(state)) {
40            return PREPARING;
41        }
42        if (!Environment.MEDIA_MOUNTED.equals(state)) {
43            return UNAVAILABLE;
44        }
45
46        File dir = new File(DIRECTORY);
47        dir.mkdirs();
48        if (!dir.isDirectory() || !dir.canWrite()) {
49            return UNAVAILABLE;
50        }
51
52        try {
53            StatFs stat = new StatFs(DIRECTORY);
54            return stat.getAvailableBlocks() * (long) stat.getBlockSize();
55        } catch (Exception e) {
56            Log.i(TAG, "Fail to access external storage", e);
57        }
58        return UNKNOWN_SIZE;
59    }
60}
61