FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SourceTree/SDCardUtils.java at develop · git-levo/SourceTree · GitHub
git-levo
/
SourceTree
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SourceTree
/
SDCardUtils.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
259 lines (256 loc) · 7.65 KB
Breadcrumbs
SourceTree
/
SDCardUtils.java
Copy path
File metadata and controls
259 lines (256 loc) · 7.65 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package
com
.
example
.
day0330_sdcard
;
import
java
.
io
.
ByteArrayOutputStream
;
import
java
.
io
.
File
;
import
java
.
io
.
FileInputStream
;
import
java
.
io
.
FileNotFoundException
;
import
java
.
io
.
FileOutputStream
;
import
java
.
io
.
InputStream
;
import
java
.
io
.
OutputStream
;
import
android
.
content
.
Context
;
import
android
.
graphics
.
Bitmap
;
import
android
.
graphics
.
BitmapFactory
;
import
android
.
os
.
Environment
;
import
android
.
os
.
StatFs
;
import
android
.
util
.
Log
;
public
class
SDCardUtils
{
//判断SD卡是否挂载
public
static
boolean
isSDCardMounted
(){
//获取SD卡外部存储状态
if
(
Environment
.
getExternalStorageState
().
equals
(
Environment
.
MEDIA_MOUNTED
)) {
return
true
;
}
return
false
;
}
//获取SD卡的根目录
public
static
String
getSDCardBaseDir
(){
if
(
isSDCardMounted
()) {
File
dir
=
Environment
.
getExternalStorageDirectory
();
return
dir
.
getAbsolutePath
();
}
return
null
;
}
//获取SD卡全部内存空间大小
public
static
long
getSDCardSize
(){
if
(
isSDCardMounted
()) {
String
dir
=
getSDCardBaseDir
();
//StatFs是从C语言引过来的
StatFs
statFs
=
new
StatFs
(
dir
);
long
blockCount
=
statFs
.
getBlockCountLong
();
//有多少块
long
blockSize
=
statFs
.
getBlockSizeLong
();
//每块有多大
return
blockCount
*
blockSize
/
1024
/
1024
;
//总大小
}
return
0
;
}
//获取SD卡空闲空间大小(有多少空间还没被占用)
public
static
long
getSDCardFreeSize
(){
if
(
isSDCardMounted
()) {
String
dir
=
getSDCardBaseDir
();
//StatFs是从C语言引过来的
StatFs
statFs
=
new
StatFs
(
dir
);
long
freeBlockCount
=
statFs
.
getFreeBlocksLong
();
//有多少块
long
blockSize
=
statFs
.
getBlockSizeLong
();
//每块有多大
return
freeBlockCount
*
blockSize
/
1024
/
1024
;
//总大小
}
return
0
;
}
//获取SD卡可用剩余空间大小(还剩下多少空间)
public
static
long
getSDCardAvailableSize
(){
if
(
isSDCardMounted
()) {
String
dir
=
getSDCardBaseDir
();
//StatFs是从C语言引过来的
StatFs
statFs
=
new
StatFs
(
dir
);
long
availableBlockCount
=
statFs
.
getAvailableBlocksLong
();
//有多少块
long
blockSize
=
statFs
.
getBlockSizeLong
();
//每块有多大
return
availableBlockCount
*
blockSize
/
1024
/
1024
;
//总大小
}
return
0
;
}
//往SD卡九大目录保存文件
public
static
boolean
saveData2SDCardpublicDir
(
byte
[]
bys
,
String
type
,
String
filename
){
if
(
isSDCardMounted
()) {
//获取SD卡的根目录
String
baseDir
=
getSDCardBaseDir
();
//获取文件的路径
String
file
=
baseDir
+
File
.
separator
+
type
+
File
.
separator
+
filename
;
try
{
OutputStream
os
=
new
FileOutputStream
(
file
);
os
.
write
(
bys
);
os
.
close
();
return
true
;
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
}
return
false
;
}
//往SDCard公有目录下保存文件 (九大公有目录中的一个,具体由type指定)
public
static
boolean
saveData2SDCardCustomDir
(
byte
[]
bys
,
String
dir
,
String
filename
){
if
(
isSDCardMounted
()) {
//获取SD卡的根目录
String
baseDir
=
getSDCardBaseDir
();
String
path
=
baseDir
+
File
.
separator
+
dir
;
//获取自建文件夹的路径
File
file1
=
new
File
(
path
);
//判断文件夹是否存在,不存在的话就得先创建
if
(!
file1
.
exists
()) {
file1
.
mkdir
();
}
//获取文件的路径
String
file
=
baseDir
+
File
.
separator
+
dir
+
File
.
separator
+
filename
;
try
{
OutputStream
os
=
new
FileOutputStream
(
file
);
os
.
write
(
bys
);
os
.
close
();
return
true
;
}
catch
(
Exception
e
) {
e
.
printStackTrace
();
}
}
return
false
;
}
//往SDCard的私有File目录下保存文件
public
static
boolean
saveData2SDCardPrivateFileDir
(
byte
[]
data
,
String
type
,
String
filename
,
Context
context
){
if
(
isSDCardMounted
()) {
File
path
=
context
.
getExternalFilesDir
(
type
);
if
(!
path
.
exists
()) {
path
.
mkdir
();
}
String
file
=
path
.
getAbsolutePath
()+
File
.
separator
+
filename
;
try
{
OutputStream
os
=
new
FileOutputStream
(
file
);
os
.
write
(
data
);
os
.
close
();
return
true
;
}
catch
(
Exception
e
) {
e
.
printStackTrace
();
}
}
return
false
;
}
//往SDCard的私有Cache目录下保存文件
public
static
boolean
saveData2SDCardPrivateCacheDir
(
byte
[]
data
,
String
filename
,
Context
context
){
if
(
isSDCardMounted
()) {
File
path
=
context
.
getExternalCacheDir
();
if
(!
path
.
exists
()) {
path
.
mkdir
();
}
String
file
=
path
.
getAbsolutePath
()+
File
.
separator
+
filename
;
try
{
OutputStream
os
=
new
FileOutputStream
(
file
);
os
.
write
(
data
);
os
.
close
();
return
true
;
}
catch
(
Exception
e
) {
e
.
printStackTrace
();
}
}
return
false
;
}
//往SDCard的私有Cache目录下保存图像
public
static
boolean
saveBitmap2SDCardPrivateCacheDir
(
Bitmap
bitmap
,
String
filename
,
Context
context
){
if
(
isSDCardMounted
()) {
File
path
=
context
.
getExternalCacheDir
();
if
(!
path
.
exists
()) {
path
.
mkdir
();
}
String
file
=
path
.
getAbsolutePath
()+
File
.
separator
+
filename
;
try
{
OutputStream
os
=
new
FileOutputStream
(
file
);
//把图片转成byte[]
//os.write();
//判断图片格式
if
(
filename
.
endsWith
(
".JPG"
)||
filename
.
endsWith
(
".jap"
)){
bitmap
.
compress
(
Bitmap
.
CompressFormat
.
JPEG
,
100
,
os
);
}
else
if
(
filename
.
endsWith
(
".PNG"
)||
filename
.
endsWith
(
".png"
)){
bitmap
.
compress
(
Bitmap
.
CompressFormat
.
PNG
,
100
,
os
);
}
os
.
close
();
return
true
;
}
catch
(
Exception
e
) {
e
.
printStackTrace
();
}
}
return
false
;
}
//从SDCard读取指定文件
public
static
byte
[]
loadFileFromSDCard
(
String
filePath
){
if
(
isSDCardMounted
()) {
String
path
=
getSDCardBaseDir
();
String
file
=
path
+
File
.
separator
+
filePath
;
try
{
InputStream
is
=
new
FileInputStream
(
file
);
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
byte
[]
bys
=
new
byte
[
1024
];
int
len
=
0
;
while
((
len
=
is
.
read
(
bys
))!=-
1
){
baos
.
write
(
bys
,
0
,
len
);
}
return
baos
.
toByteArray
();
}
catch
(
Exception
e
) {
e
.
printStackTrace
();
}
}
return
null
;
}
//从SDCard读取Bitmap并返回
public
static
Bitmap
loadBitmapFromSDCard
(
String
filePath
){
if
(
isSDCardMounted
()) {
//filePath就是全路径
try
{
InputStream
is
=
new
FileInputStream
(
filePath
);
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
byte
[]
bys
=
new
byte
[
1024
];
int
len
=
0
;
while
((
len
=
is
.
read
(
bys
))!=-
1
){
baos
.
write
(
bys
,
0
,
len
);
}
byte
[]
data
=
baos
.
toByteArray
();
Bitmap
bitmap
=
BitmapFactory
.
decodeByteArray
(
data
,
0
,
data
.
length
);
return
bitmap
;
}
catch
(
Exception
e
) {
e
.
printStackTrace
();
}
}
return
null
;
}
//获取SD卡公有目录路径
public
static
String
getSDCardPublicDir
(
String
type
){
if
(
isSDCardMounted
()) {
File
dir
=
Environment
.
getExternalStoragePublicDirectory
(
type
);
return
dir
.
getAbsolutePath
();
}
return
null
;
}
//获取SDCard私有Cache目录路径
public
static
String
getSDCardPrivateCacheDir
(
Context
context
){
if
(
isSDCardMounted
()) {
File
dir
=
context
.
getExternalCacheDir
();
return
dir
.
getAbsolutePath
();
}
return
null
;
}
//获取SDCard私有File目录路径
public
static
String
getSDCardPrivateFilesDir
(
Context
context
,
String
type
){
if
(
isSDCardMounted
()) {
File
dir
=
context
.
getExternalFilesDir
(
type
);
return
dir
.
getAbsolutePath
();
}
return
null
;
}
//判断一个文件是否存在
public
static
boolean
isFileExists
(
String
filePath
){
if
(
isSDCardMounted
()) {
File
file
=
new
File
(
filePath
);
return
file
.
exists
();
}
return
false
;
}
//删除一个文件
public
static
boolean
removeFileFromSDCard
(
String
filePath
){
if
(
isSDCardMounted
()) {
File
file
=
new
File
(
filePath
);
return
file
.
delete
();
}
return
false
;
}
}
Back
|
FazBrowse Home
|
New Git URL