Storage.java
5.66 KB
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
package com.sw.laryngoscope.utils;
import android.content.ContentResolver;
import android.location.Location;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;
import com.sw.laryngoscope.utils.exif.ExifInterface;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Storage {
private static final String TAG = "Storage";
public static final String DCIM =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString();
public static String VIDEO_DIR = DCIM + "/OCamera";
public static String VIDEO_THUMB = DCIM + "/OVideoThumbnail";
public static String PIC_DIR = DCIM + "/OPhoto";
public static String PDF_DIR = DCIM + "/OPdf";
public static String PDF_THUMB_DIR = DCIM + "/OPdf_Thumb";
public static String PIC_ICE_NAME = "ice";
public static String PIC_ICE_PATH = PIC_DIR + "/" + PIC_ICE_NAME + ".jpg";
public static final long UNAVAILABLE = -1L;
public static final long PREPARING = -2L;
public static final long UNKNOWN_SIZE = -3L;
public static final long LOW_STORAGE_THRESHOLD_BYTES = 50 * 1024 * 1024;
public static long getAvailableSpace() {
String state = Environment.getExternalStorageState();
Logger.d("External storage state=" + state);
if (Environment.MEDIA_CHECKING.equals(state)) {
return PREPARING;
}
if (!Environment.MEDIA_MOUNTED.equals(state)) {
return UNAVAILABLE;
}
File dir = new File(DCIM);
//dir.mkdirs();
new File(VIDEO_DIR).mkdirs();
new File(VIDEO_THUMB).mkdirs();
new File(PIC_DIR).mkdirs();
new File(PDF_DIR).mkdirs();
new File(PDF_THUMB_DIR).mkdirs();
if (!dir.isDirectory() || !dir.canWrite()) {
Logger.d("DIRECTORY:" + dir.getPath() + " UNAVAILABLE");
return UNAVAILABLE;
}
try {
StatFs stat = new StatFs(DCIM);
return stat.getAvailableBlocks() * (long) stat.getBlockSize();
} catch (Exception e) {
Logger.d("Fail to access external storage" + e);
}
return UNKNOWN_SIZE;
}
public static final String MIME_TYPE_JPEG = "image/jpeg";
public static final String MIME_TYPE_GIF = "image/gif";
public static final String JPEG_POSTFIX = ".jpg";
public static final String GIF_POSTFIX = ".gif";
public static final String PNG_POSTFIX = ".png";
public static final String PDF_POSTFIX = ".pdf";
public static final String MP4_POSTFIX = ".mp4";
public static String addImage(ContentResolver resolver, String title, long date,
Location location, int orientation, ExifInterface exif, byte[] data, int width,
int height, String mimeType) throws IOException {
String path = generateFilepath(title, mimeType);
long fileLength = writeFile(path, data, exif);
if (fileLength >= 0) {
return path;
}
return null;
}
private static String generateFilepath(String title, String mimeType) {
return generateFilepath(PIC_DIR, title, mimeType);
}
public static String generateFilepath(String directory, String title, String mimeType) {
String extension = null;
if (MIME_TYPE_JPEG.equals(mimeType)) {
extension = JPEG_POSTFIX;
} else if (MIME_TYPE_GIF.equals(mimeType)) {
extension = GIF_POSTFIX;
} else {
throw new IllegalArgumentException("Invalid mimeType: " + mimeType);
}
return (new File(directory, title + extension)).getAbsolutePath();
}
public static long writeFile(String path, byte[] jpeg, ExifInterface exif) throws IOException {
if (!createDirectoryIfNeeded(path)) {
Log.e(TAG, "Failed to create parent directory for file: " + path);
return -1;
}
if (exif != null) {
//exif.setTagValue(ExifInterface.TAG_ORIENTATION, 180);
/*Bitmap realImage = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length);
Matrix mat = new Matrix();
mat.postRotate(180);
Bitmap mBitmap = Bitmap.createBitmap(realImage, 0, 0, realImage.getWidth(), realImage.getHeight(), mat, true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
exif.writeExif(stream.toByteArray(), path);*/
exif.writeExif(jpeg, path);
File f = new File(path);
return f.length();
} else {
return writeFile(path, jpeg);
}
// return -1;
}
private static boolean createDirectoryIfNeeded(String filePath) {
File parentFile = new File(filePath).getParentFile();
// If the parent exists, return 'true' if it is a directory. If it's a
// file, return 'false'.
if (parentFile.exists()) {
return parentFile.isDirectory();
}
// If the parent does not exists, attempt to create it and return
// whether creating it succeeded.
return parentFile.mkdirs();
}
private static long writeFile(String path, byte[] data) {
FileOutputStream out = null;
try {
out = new FileOutputStream(path);
out.write(data);
return data.length;
} catch (Exception e) {
Log.e(TAG, "Failed to write data", e);
} finally {
try {
out.close();
} catch (Exception e) {
Log.e(TAG, "Failed to close file after write", e);
}
}
return -1;
}
}