Android中操作 SDCard文件
1 import android.content.Context;
2 import android.graphics.Bitmap;
3 import android.graphics.BitmapFactory;
4 import android.os.Environment;
5 import android.os.StatFs;
6 import android.util.Log;
7
8 import java.io.BufferedInputStream;
9 import java.io.BufferedOutputStream;
10 import java.io.ByteArrayOutputStream;
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15
16 public class SDCardHelper {
17
18 // 往SD卡的私有Files目录下保存文件
19 public static void saveFileToSDCardPrivateFilesDir(Context context, String data, String type, String fileName, boolean append) throws Exception {
20 BufferedOutputStream bos = null;
21 {
22 try {
23 File file = context.getExternalFilesDir(type);
24
25 bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName), append));
26 bos.write(ComFn.stringTobytes(data) );
27 bos.flush();
28
29 Log.e("Trancy Save "+fileName+" Data: ", data);
30 } catch (Exception e) {
31 e.printStackTrace();
32 throw e;
33 } finally {
34 try {
35 if(bos != null) {
36 bos.close();
37 }
38 } catch (IOException e) {
39 e.printStackTrace();
40 }
41 }
42 }
43 }
44
45 // 从SD卡获取文件
46 public static String loadFileFromSDCard(String filePath) throws Exception{
47 BufferedInputStream bis = null;
48 ByteArrayOutputStream baos = new ByteArrayOutputStream();
49
50 try {
51 File file = new File(filePath);
52 if(!file.exists()){
53 return "";
54 }
55
56 bis = new BufferedInputStream(new FileInputStream(file));
57 byte[] buffer = new byte[8 * 1024];
58 int c = 0;
59 while ((c = bis.read(buffer)) != -1) {
60 baos.write(buffer, 0, c);
61 baos.flush();
62 }
63
64 Log.e("Trancy Load "+filePath+" Data: ", baos.toString());
65 return baos.toString();
66 } catch (Exception e) {
67 e.printStackTrace();
68 throw e;
69 } finally {
70 try {
71 if(baos!=null) {
72 baos.close();
73 }
74 if(bis!=null) {
75 bis.close();
76 }
77 } catch (IOException e) {
78 e.printStackTrace();
79 throw e;
80 }
81 }
82 }
83
84 // 从sdcard中删除文件
85 public static boolean removeFileFromSDCard(String filePath) {
86 File file = new File(filePath);
87 if (file.exists()) {
88 try {
89 file.delete();
90 return true;
91 } catch (Exception e) {
92 return false;
93 }
94 } else {
95 return false;
96 }
97 }
98
99 // 判断SD卡是否被挂载
100 public static boolean isSDCardMounted() {
101 // return Environment.getExternalStorageState().equals("mounted");
102 return Environment.getExternalStorageState().equals(
103 Environment.MEDIA_MOUNTED);
104 }
105
106 // 获取SD卡的根目录
107 public static String getSDCardBaseDir() {
108 if (isSDCardMounted()) {
109 return Environment.getExternalStorageDirectory().getAbsolutePath();
110 }
111 return null;
112 }
113
114 public static String getSDCardBaseDiraa(Context context) {
115
116 if (isSDCardMounted()) {
117 return Environment.getExternalStorageDirectory().getAbsolutePath();
118 }
119 return null;
120 }
121
122
123 // 获取SD卡的完整空间大小,返回MB
124 public static long getSDCardSize() {
125 if (isSDCardMounted()) {
126 StatFs fs = new StatFs(getSDCardBaseDir());
127 long count = fs.getBlockCountLong();
128 long size = fs.getBlockSizeLong();
129 return count * size / 1024 / 1024;
130 }
131 return 0;
132 }
133
134 // 获取SD卡的剩余空间大小
135 public static long getSDCardFreeSize() {
136 if (isSDCardMounted()) {
137 StatFs fs = new StatFs(getSDCardBaseDir());
138 long count = fs.getFreeBlocksLong();
139 long size = fs.getBlockSizeLong();
140 return count * size / 1024 / 1024;
141 }
142 return 0;
143 }
144
145 // 获取SD卡的可用空间大小
146 public static long getSDCardAvailableSize() {
147 if (isSDCardMounted()) {
148 StatFs fs = new StatFs(getSDCardBaseDir());
149 long count = fs.getAvailableBlocksLong();
150 long size = fs.getBlockSizeLong();
151 return count * size / 1024 / 1024;
152 }
153 return 0;
154 }
155
156 // 往SD卡的公有目录下保存文件
157 public static boolean saveFileToSDCardPublicDir(byte[] data, String type, String fileName) {
158 BufferedOutputStream bos = null;
159 if (isSDCardMounted()) {
160 File file = Environment.getExternalStoragePublicDirectory(type);
161 try {
162 bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
163 bos.write(data);
164 bos.flush();
165 return true;
166 } catch (Exception e) {
167 e.printStackTrace();
168 } finally {
169 try {
170 bos.close();
171 } catch (IOException e) {
172 // TODO Auto-generated catch block
173 e.printStackTrace();
174 }
175 }
176 }
177 return false;
178 }
179
180 // 往SD卡的自定义目录下保存文件
181 public static boolean saveFileToSDCardCustomDir(byte[] data, String dir, String fileName) {
182 BufferedOutputStream bos = null;
183 if (isSDCardMounted()) {
184 File file = new File(getSDCardBaseDir() + File.separator + dir);
185 if (!file.exists()) {
186 file.mkdirs();// 递归创建自定义目录
187 }
188 try {
189 bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
190 bos.write(data);
191 bos.flush();
192 return true;
193 } catch (Exception e) {
194 e.printStackTrace();
195 } finally {
196 try {
197 bos.close();
198 } catch (IOException e) {
199 // TODO Auto-generated catch block
200 e.printStackTrace();
201 }
202 }
203 }
204 return false;
205 }
206
207 // 往SD卡的私有Cache目录下保存文件
208 public static boolean saveFileToSDCardPrivateCacheDir(byte[] data, String fileName, Context context) {
209 BufferedOutputStream bos = null;
210 if (isSDCardMounted()) {
211 File file = context.getExternalCacheDir();
212 try {
213 bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
214 bos.write(data);
215 bos.flush();
216 return true;
217 } catch (Exception e) {
218 e.printStackTrace();
219 } finally {
220 try {
221 bos.close();
222 } catch (IOException e) {
223 // TODO Auto-generated catch block
224 e.printStackTrace();
225 }
226 }
227 }
228 return false;
229 }
230
231 // 保存bitmap图片到SDCard的私有Cache目录
232 public static boolean saveBitmapToSDCardPrivateCacheDir(Bitmap bitmap, String fileName, Context context) {
233 if (isSDCardMounted()) {
234 BufferedOutputStream bos = null;
235 // 获取私有的Cache缓存目录
236 File file = context.getExternalCacheDir();
237
238 try {
239 bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
240 if (fileName != null && (fileName.contains(".png") || fileName.contains(".PNG"))) {
241 bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
242 } else {
243 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
244 }
245 bos.flush();
246 } catch (Exception e) {
247 e.printStackTrace();
248 } finally {
249 if (bos != null) {
250 try {
251 bos.close();
252 } catch (IOException e) {
253 e.printStackTrace();
254 }
255 }
256 }
257 return true;
258 } else {
259 return false;
260 }
261 }
262
263 // 从SDCard中寻找指定目录下的文件,返回Bitmap
264 public Bitmap loadBitmapFromSDCard(String filePath) throws Exception {
265 byte[] data = loadFileFromSDCard(filePath).getBytes();
266 if (data != null) {
267 Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
268 if (bm != null) {
269 return bm;
270 }
271 }
272 return null;
273 }
274
275 // 获取SD卡公有目录的路径
276 public static String getSDCardPublicDir(String type) {
277 return Environment.getExternalStoragePublicDirectory(type).toString();
278 }
279
280 // 获取SD卡私有Cache目录的路径
281 public static String getSDCardPrivateCacheDir(Context context) {
282 return context.getExternalCacheDir().getAbsolutePath();
283 }
284
285 // 获取SD卡私有Files目录的路径
286 public static String getSDCardPrivateFilesDir(Context context, String type) {
287 return context.getExternalFilesDir(type).getAbsolutePath();
288 }
289
290 public static boolean isFileExist(String filePath) {
291 File file = new File(filePath);
292 return file.isFile();
293 }
294 }
Android中操作 SDCard文件的更多相关文章
- android 中获取视频文件的缩略图(非原创)
在android中获取视频文件的缩略图有三种方法: 1.从媒体库中查询 2. android 2.2以后使用ThumbnailUtils类获取 3.调用jni文件,实现MediaMetadataRet ...
- Android中使用File文件进行数据存储
Android中使用File文件进行数据存储 上一篇学到使用SharedPerences进行数据存储,接下来学习一下使用File进行存储 我们有时候可以将数据直接以文件的形式保存在设备中, 例如:文本 ...
- 修改Android中strings.xml文件, 动态改变数据
有些朋友可能会动态的修改Android中strings.xml文件中的值,在这里给大家推荐一种简单的方法.strings.xml中节点是支持占位符的,如下所示: <string name=&qu ...
- Android中得到布局文件对象有三种方式
Android中得到布局文件对象有三种方式 第一种,通过Activity对象 View view = Activity对象.getLayoutInflater().inflater(R.layout. ...
- Android中的gen文件为空或者不存在的处理方法
Android中的gen文件时链接程序和XML中资源定义的桥梁,所以如果gen文件夹为空可能有以下的几个原因: 1.XML文件错误,这时可以检查res文件夹中的文件是否有错误 2.导入新的Androi ...
- python中操作csv文件
python中操作csv文件 读取csv improt csv f = csv.reader(open("文件路径","r")) for i in f: pri ...
- Android中使用SDcard进行文件的读取
来自:http://www.cnblogs.com/greatverve/archive/2012/01/13/android-SDcard.html 平时我们需要在手机上面存储想音频,视频等等的大文 ...
- Android中在sdcard上创建文件夹
//在SD卡上创建一个文件夹 public void createSDCardDir(){ if(Environment.MEDIA_MOUNTED.equals(Environment ...
- Android中访问sdcard路径的几种方式
以前的Android(4.1之前的版本)中,SDcard路径通过"/sdcard"或者"/mnt/sdcard"来表示,而在JellyBean(安卓4.1)系统 ...
- Android中的资源文件
最近复习Android资源文件的内容,留下点记录以备后用. Android中的资源主要是指存放在应用程序或者Framework相应包下/res中的内容.它们可以被本地化,如果必要的话会被编译成二进制文 ...
随机推荐
- 西湖论剑2023-mp3[wp]
一 题目描述 二 解题步骤 1.分析文件 (1)放入Audacity中查看频谱信息无果 (2)010editor中查看文件结构 文件尾部存在PNG文件尾,搜索png文件头 将该png文件复制提取出来, ...
- jenkins目录
Jenkins目录详解: jobs目录:创建的所有jenkins工程.并含有所有构建历史记录和日志.其中config.xml为具体配置. plugins:所有插件 workspace:构建工程本机源码 ...
- LESS-8
根据题目,这是一道布尔型注入.页面只有返回正常和不正常两种. payload: ' and substr(database(),1,1)='s' --+ 判断当前数据库名的第一个字母,是's'页面就 ...
- Django 初步运行过程分析笔记
2. django运行过程分析第一个过程分析:django启动过程python mangage.py runserver 0.0.0.0:8000这个命令先被python的sys.argv接收起来,保 ...
- Angular单页应用程式 (SPA)+Azure AD重新导向登入
一.app.module.ts中设定应用程式 1.将MSAL Angular相关设置封装为auth.module.ts import { NgModule } from '@angular/core' ...
- VS2022在打开设计器的时候提示某变量未声明或者未赋值
有可能是在属性中直接添加引用的dll文件,导致dll并不能被该文件稳定引用,可以删除引用,重新添加试试
- linux下生成证书
1.生成私有证书 # 生成需要密码的密钥文件server.key openssl genrsa -des3 -out server.key 2048 # 转成不用密码的rsa密钥文件 openssl ...
- MSSQL执行超大.sql脚本
1.打开mssql安装路径:找到Microsoft SQL Server Management Studio的图标,点击右键属性>打开文件位置 2.在安装路径下打开cmd控制台 3.输入命令: ...
- Kubernetes--Pod对象的生命周期
Pod对象自从其创建开始至其终止退出的时间范围称为其生命周期.在这段时间中,Pod会处于多种不同的状态,并执行一些操作:其中,创建主容器(main container)为必需的操作,其他可选的操作还包 ...
- PyTorch之初级使用
使用流程①. 数据准备; ②. 模型确立; ③. 损失函数确立; ④. 优化器确立; ⑤. 模型训练及保存 模块介绍Part Ⅰ: 数据准备torch.utils.data.Dataset torch ...