SD卡操作
一.手机内存图
2.SD卡介绍:
1.一般手机文件管理 根路径 /storage/emulated/0/或者/mnt/shell/emulated/0
2.重要代码:
(1)Environment.getExternalStorageState();// 判断SD卡是否
(2)Environment.getExternalStorageDirectory(); 获取SD卡的根目录
(3)Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 获取SD卡公开目录pictures文件夹
3.必须要添加读写SD卡的权限
三.代码
(1)添加读写SD卡的 权限
(2)FileUtils.java:四个方法:实现向SD卡中读写Bitmap图片和json字符串
public class FileUtils {
//方法1:向SD卡中写json串
public static void write_json(String json) {
//判断是否挂载
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//获取SD卡根路径:mnt/shell/emulated/0
File file=Environment.getExternalStorageDirectory();
FileOutputStream out=null;
try {
//创建输出流
out= new FileOutputStream(new File(file,"json.txt"));
out.write(json.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//方法2:从SD卡中读取json串
public static String read_json() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File file = Environment.getExternalStorageDirectory();
FileInputStream inputStream = null;
StringBuffer sb=new StringBuffer();
try {
inputStream=new FileInputStream(new File(file,"json.txt"));
byte[] b=new byte[1024];
int len=0;
while((len=inputStream.read(b))!=-1){
sb.append(new String(b,0,len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}else{
return "";
}
}
//方法3:从SD卡中读取一张图片
public static Bitmap read_bitmap(String filename) {//filename图片名字
Bitmap bitmap=null;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file=Environment.getExternalStorageDirectory();
File file1 = new File(file, filename);
//BitmapFactory可以直接根据SD卡图片路径转成一个bitmap对象
bitmap= BitmapFactory.decodeFile(file1.getAbsolutePath());
}
return bitmap;
}
//方法4:网络下载一张图片存储到SD卡中
public static void write_bitmap(String url) {//网址
new MyTask().execute(url);
}
static class MyTask extends AsyncTask<String,String,String> {
@Override
protected String doInBackground(String... strings) {
FileOutputStream out=null;
InputStream inputStream=null;//网络连接的输入流
HttpURLConnection connection=null;//向SD卡写的输出流
try {
URL url= new URL(strings[0]);
connection= (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5*1000);
connection.setReadTimeout(5*1000);
if (connection.getResponseCode()==200){
inputStream = connection.getInputStream();
//TODO 获取SD卡的路径
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//是否挂载
File file = Environment.getExternalStorageDirectory();
out = new FileOutputStream(new File(file,"xiaoyueyue.jpg"));
byte[] bytes=new byte[1024];
int len=0;
while((len=inputStream.read(bytes))!=-1){
out.write(bytes,0,len);
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//关流
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(connection!=null){
connection.disconnect();
}
}
return null;
}
}
}