1. 程式人生 > >第5天SD卡操作

第5天SD卡操作

第5天SD卡操作

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; } } }