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 ...
随机推荐
- 自己编写的一个有关安卓应用开发培训PPT
这个是我自己编写的一个有关安卓应用开发培训PPT,适合新手. 在这里下载PPT
- lnmp平台菜鸟入门级笔记
LNMP平台搭建 Mysql安装 MySQL安装 回复收藏 分享 1 下载MySQL数据库l到/usr/local/src/[root@xin tmp]# cd ...
- IOS开发之网络编程开源类 Reachability应用
先看Reachability.h发现 #import <Foundation/Foundation.h> #import <SystemConfiguration/SystemCon ...
- Nim Game,Reverse String,Sum of Two Integers
下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap o ...
- 解决在VS2015下用C++开发的DLL在WIN7上无法加载运行
首先用Dependency Walker检查该DLL依赖的库,如下图所示: 依赖的动态库除了KERNEL32.DLL.USER32.DLL外,还包括了MSVCP120D.DLL以及MSVCR120D. ...
- SQLServer2005删除log文件和清空日志的方案
数据库在使用过程中会使日志文件不断增加,使得数据库的性能下降,并且占用大量的磁盘空间.SQL Server数据库都有log文件,log文件记录用户对数据库修改的操作.可以通过直接删除log文件和清空日 ...
- maven 项目无法发布,无法编译的解决办法
1 Web Deployment Assembly信息都合理2 重新clear项目,让JAVA代码重新生成.class文件在target目录中
- FlashBuilder 新建项目时提示 java.lang.nullpointerexception
可以尝试安装 Air SDK
- Async Programming - 1 async-await 糖的本质(2)
上一篇讲了这么多,其实说的就是一个事,return会被编译器重写成SetResult,所以如果我们的异步函数返回的是一个Task<int>,代码就要改成这样: using System; ...
- React组件的分类
* 1.statelessComponent 不包含任何state的组件 例如:AntDesign的 :Button,Input组件 * 2.viewComponent 包含少量ui state的组件 ...