SDcard
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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; public class SDCardUtils { // 判断SDCard是否挂载
public static boolean isSDCardMounted() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return true;
}
return false;
} // 获得SDCard的根目录 如/storage/sdcard/
public static String getSDCardBaseDir() {
if (isSDCardMounted()) {
File dir = Environment.getExternalStorageDirectory();
return dir.getAbsolutePath();
}
return null;
} // 获得SD卡的全部空间大小(单位:M)
public static long getSDCardSize() {
if (isSDCardMounted()) {
String dir = getSDCardBaseDir();
StatFs statFs = new StatFs(dir); // StatFs是C语言引入过来的
long blockCount = statFs.getBlockCountLong(); // 有多少块
long blockSize = statFs.getBlockSizeLong(); // 每块有多大
return blockCount * blockSize / 1024 / 1024;
}
return 0;
} // 获取SDCard空闲空间的大小(单位:M) (有多少空间还没被占用的)
public static long getSDCardFreeSize() {
if (isSDCardMounted()) {
String dir = getSDCardBaseDir();
StatFs statFs = new StatFs(dir); // StatFs是C语言引入过来的
long freeBlockCount = statFs.getFreeBlocksLong(); // 空闲的块
long blockSize = statFs.getBlockSizeLong(); // 每块有多大
return freeBlockCount * blockSize / 1024 / 1024;
}
return 0;
} // 获取SDCard可用空间的大小(单位:M) (有多少空间你可以使用的)
public static long getSDCardAvailSize() {
if (isSDCardMounted()) {
String dir = getSDCardBaseDir();
StatFs statFs = new StatFs(dir); // StatFs是C语言引入过来的
long availBlockCount = statFs.getAvailableBlocksLong(); // 可用的块
long blockSize = statFs.getBlockSizeLong(); // 每块有多大
return availBlockCount * blockSize / 1024 / 1024;
}
return 0;
} // 往SDCard公有目录下保存文件 (九大公有目录中的一个,具体由type指定) /storage/sdcard/{type}
public static boolean saveData2SDCardPublicDir(byte[] data, String type, String filename) {
if (isSDCardMounted()) {
// 文件名:/storage/sdcard/Musics/111.txt
// 文件名:getSDCardBaseDir()/{type}/{filename}
String baseDir = getSDCardBaseDir();
String file = baseDir + File.separator + type + "/" + filename;
try {
OutputStream os = new FileOutputStream(file);
os.write(data);
os.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
} // 往SDCard的自定义目录中保存数据 /storage/sdcard/{dir}
public static boolean saveData2SDCardCustomDir(byte[] data, String dir, String filename) {
if (isSDCardMounted()) {
// /storage/sdcard/{dir}
// getSDCardBaseDir()/{dir}/{filename}
String baseDir = getSDCardBaseDir(); // SDCard目录
String path = baseDir + "/" + dir; // SDCard中自定义的目录
// 如果path不存在的话 需要创建目录
File customPath = new File(path);
if (!customPath.exists()) {
customPath.mkdir();
}
String file = path + "/" + filename; // SDCard中自定义目录中的文件
try {
OutputStream os = new FileOutputStream(file);
os.write(data);
os.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
return false;
} // :往SDCard的私有File目录下保存文件
// /storage/sdcard/Android/data/包名/files/{type}/{filename}
public static boolean saveData2SDCardPrivateFileDir(byte[] data, String type, String filename, Context context) {
if (isSDCardMounted()) {
// storage/sdcard/Android/data/包名/files/{type}/{filename}
File path = context.getExternalFilesDir(type);
if (!path.exists()) {
path.mkdir();
}
String file = path.getAbsolutePath() + "/" + filename; // 文件路径
try {
OutputStream os = new FileOutputStream(file);
os.write(data);
os.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
} // 往SDCard的私有Cache目录下保存文件 /storage/sdcard/Android/data/包名/cache/{filename}
public static boolean saveData2SDCardPrivateCacheDir(byte[] data, String filename, Context context) {
if (isSDCardMounted()) {
// storage/sdcard/Android/data/包名/cache/{filename}
File path = context.getExternalCacheDir();
if (!path.exists()) {
path.mkdir();
}
String file = path.getAbsolutePath() + "/" + filename;
try {
OutputStream os = new FileOutputStream(file);
os.write(data);
os.close();
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
} // 往SDCard的私有Cache目录下保存图像 /storage/sdcard/Android/data/包名/cache/{filename} xxx.png xxx.jpg
public static boolean saveBitmap2SDCardPrivateCacheDir(Bitmap bitmap,
String filename, Context context) {
if(isSDCardMounted()) {
// 路径/storage/sdcard/Android/data/包名/cache/{filename}
File path = context.getExternalCacheDir();
if(!path.exists()) {
path.mkdir();
}
String file = path.getAbsolutePath() + "/" + filename; // bmp、jpg
try {
OutputStream os = new FileOutputStream(file);
// 图片转换成byte[] 下面的代码代替os.write(...);
// 判断到底是哪种图片的格式(jpg、png)
if(filename.endsWith(".jpg") || filename.endsWith(".JPG")) {
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 (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} return false;
} // 从SDCard读取指定文件 /storage/sdcard/{filePath}
public static byte[] loadFileFromSDCard(String filePath) {
if(isSDCardMounted()) {
// /storage/sdcard/{filePath}
// getSDCardBaseDir()/{filePath}
String path = getSDCardBaseDir();
String file = path + "/" + filePath;
try {
InputStream is = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int ret;
while(true) {
ret = is.read(buffer);
if(ret < 0) {
break;
}
baos.write(buffer, 0, ret);
}
return baos.toByteArray(); // 返回字节数组
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
} // 从SDCard读取Bitmap并返回 /storage/sdcard/{filePath}
public static Bitmap loadBitmapFromSDCard(String filePath) {
if(isSDCardMounted()) {
// 由于传入的参数已经是全路径了 因此不需要加上getSDCardBaseDir()
try {
InputStream is = new FileInputStream(filePath);
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ret;
while(true) {
ret = is.read(buffer);
if(ret < 0) {
break;
}
baos.write(buffer, 0, ret);
}
byte[] data = baos.toByteArray();
return BitmapFactory.decodeByteArray(data, 0, data.length);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
SDcard的更多相关文章
- Android中访问sdcard路径的几种方式
以前的Android(4.1之前的版本)中,SDcard路径通过"/sdcard"或者"/mnt/sdcard"来表示,而在JellyBean(安卓4.1)系统 ...
- Android简单介绍SharedPreference,内部文件,sdcard数据存储
SharedPreference 以xml的结构储存简单的数据,储存在data/data/程序包名/shared_prefs文件夹中 使用方式 创建对象的方式有三种 Context 的 getShar ...
- Android获取内置sdcard跟外置sdcard路径
Android获取内置sdcard跟外置sdcard路径.(测试过两个手机,亲测可用) 1.先得到外置sdcard路径,这个接口是系统提供的标准接口. 2.得到上一级文件夹目录 3.得到该目录的所有文 ...
- sp,文件以及SDcard存储
XML: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" androi ...
- Android:StatFs类 获取系统/sdcard存储空间信息
在存储文件时,为了保证有充足的存储空间大小,通常需要知道系统内部或者sdcard的剩余存储空间大小,这里就需要用到StatFs类. 1. 判断 SDCard 是否存在,并且是否具有可读写权限 /** ...
- Get sdcard directory by adb
解决方案: adb shell echo $EXTERNAL_STORAGE I am making an application, which pulls files(Saved by andro ...
- 【Android】中兴ZTE sdcard路径的问题
测试机: ZTE U950 现象: 用Environment.getExternalStorageDirectory()取到的路径是/mnt/sdcard 真相: /mnt/sdcard/是一个空文件 ...
- Android中在sdcard上创建文件夹
//在SD卡上创建一个文件夹 public void createSDCardDir(){ if(Environment.MEDIA_MOUNTED.equals(Environment ...
- 保存字符串到手机SDcard为txt文件
try { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File sdCardDir ...
随机推荐
- MySQL Innodb数据库性能实践——热点数据性能
摘要: 对于大部分的应用来说,都存在热点数据的访问,即:某些数据在一定时间内的访问频率要远远高于其它数据. 常见的热点数据有“最新的新闻”.“最热门的新闻”.“下载量最大”的电影等. 为了了解MySQ ...
- excel 导入数值变成科学记数的解决办法.
string conn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR=Yes;IM ...
- [Andriod] - Andriod Studio + 逍遥模拟器
Andriod Studio自身自带的模拟器实在太卡,用Genymotion模拟器又要安装VirtualBox,然后一堆的设置,结果还是卡B. 网上下了个逍遥模拟器,这模拟器是游戏专用的,目前正式版的 ...
- 【MySQL】漫谈MySQL中的事务及其实现
最近一直在做订单类的项目,使用了事务.我们的数据库选用的是MySQL,存储引擎选用innoDB,innoDB对事务有着良好的支持.这篇文章我们一起来扒一扒事务相关的知识. 为什么要有事务? 事务广泛的 ...
- NLog学习
一.什么是NLog? NLog((http://www.nlog-project.org)是一个基于.NET平台编写的类库,我们可以使用NLog在应用程序中添加极为完善的跟踪调试代码. NLog允许我 ...
- [译]How to Install Node.js on Ubuntu 14.04 如何在ubuntu14.04上安装node.js
原文链接为 http://www.hostingadvice.com/how-to/install-nodejs-ubuntu-14-04/ 由作者Jacob Nicholson 发表于October ...
- (error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about t
运行redis过程中,突然报错如下: (error) MISCONF Redis is configured to save RDB snapshots, but is currently not a ...
- Oracle补习班第十天
Life without love is like a tree without blossoms or fruit. 缺少爱的生活就像从未开花结果的枯树 RMAN备份工具 crosscheck ba ...
- [Mongdb] 关于Replica Set复制集奇数成员限制的解释--待完善
一.缘由: http://blog.itpub.net/29254281/viewspace-1176821/ http://blog.chinaunix.net/uid-20726500-id-54 ...
- 查看sql语句执行的消耗
set statistics profile on set statistics io on set statistics time on go <这里写上你的语句...> go set ...