获取android的SDK或者手机目录路径
生活随笔
收集整理的這篇文章主要介紹了
获取android的SDK或者手机目录路径
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
獲取android的SDK或者手機目錄路徑
Google為我們提供了API來獲取SDK或者手機目錄路徑:
1、獲取SD卡目錄
File file1 = Environment.getExternalStorageDirectory();
2、獲取手機內部存儲空間的file目錄
File file2 = getFilesDir();
3、獲取內部存儲空間的緩存目錄
File file3 = getCacheDir();
4、檢查SD是否被掛載
String state = Environment.getExternalStorageState();
如果 state==“mounted” 表示被掛載
?
代碼:
com.example.readwrite.MainActivity
1 package com.example.readwrite; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 import android.app.Activity; 9 import android.os.Bundle; 10 import android.os.Environment; 11 import android.util.Log; 12 13 /** 14 * 正斜杠代表根目錄 兩種最常見的數據存儲方式 15 * 16 * 一、內存 二、本地 1.手機內部存儲 2.外部存儲設備(SD卡) 17 * */ 18 public class MainActivity extends Activity { 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 // existSDcard(); 25 // write(); 26 listPath(); 27 //read(); 28 } 29 30 private void write() { 31 // /mnt/sdcard 32 File file = Environment.getExternalStorageDirectory(); 33 FileOutputStream out = null; 34 try { 35 out = new FileOutputStream(file.getPath() + "/fanfan.txt"); 36 // out = new FileOutputStream( 37 // "/data/data/com.example.readwrite/fanfan.txt"); 38 out.write("12345".getBytes()); 39 } catch (IOException e) { 40 e.printStackTrace(); 41 } finally { 42 if (out != null) { 43 try { 44 out.close(); 45 } catch (IOException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 48 } 49 } 50 } 51 } 52 53 private void read() { 54 FileInputStream in = null; 55 try { 56 // in = new FileInputStream("/mnt/sdcard/fanfan.txt"); 57 in = new FileInputStream( 58 "/data/data/com.jiguang.test/databases/rep.db"); 59 byte[] bytes = new byte[2014]; 60 int len = in.read(bytes); 61 String str = new String(bytes, 0, len); 62 Log.d("fanfan", "---------" + str); 63 } catch (IOException e) { 64 Log.d("fanfan","報錯啦"+e.toString()); 65 } finally { 66 if (in != null) { 67 try { 68 in.close(); 69 } catch (IOException e) { 70 e.printStackTrace(); 71 } 72 } 73 } 74 } 75 76 /** 77 * 檢查SD卡是否被掛載 78 * */ 79 private void existSDcard() { 80 // 獲取SD卡的狀態 81 String state = Environment.getExternalStorageState(); 82 83 if (Environment.MEDIA_MOUNTED.equals(state)) { 84 Log.d("fanfan", "有SD卡"); 85 } else { 86 Log.d("fanfan", "沒有SD卡"); 87 } 88 } 89 90 /** 91 * 通過API獲取路徑 92 * */ 93 private void listPath() { 94 // 獲取SD卡目錄 95 File file1 = Environment.getExternalStorageDirectory(); 96 Log.d("fanfan", "sd卡----" + file1.getPath()); 97 // 獲取手機內部存儲空間的file目錄 98 File file2 = getFilesDir(); 99 Log.d("fanfan", "內部存儲File----" + file2.getPath()); 100 // 獲取內部存儲空間的緩存目錄 101 File file3 = getCacheDir(); 102 Log.d("fanfan", "內部存儲緩存目錄----" + file3.getPath()); 103 } 104 } 獲取路徑 1 /** 2 * 檢查SD卡是否被掛載 3 * */ 4 private void existSDcard() { 5 // 獲取SD卡的狀態 6 String state = Environment.getExternalStorageState(); 7 8 if (Environment.MEDIA_MOUNTED.equals(state)) { 9 Log.d("fanfan", "有SD卡"); 10 } else { 11 Log.d("fanfan", "沒有SD卡"); 12 } 13 } 檢查SD卡是否被掛載 1 /** 2 * 通過API獲取路徑 3 * */ 4 private void listPath() { 5 // 獲取SD卡目錄 6 File file1 = Environment.getExternalStorageDirectory(); 7 Log.d("fanfan", "sd卡----" + file1.getPath()); 8 // 獲取手機內部存儲空間的file目錄 9 File file2 = getFilesDir(); 10 Log.d("fanfan", "內部存儲File----" + file2.getPath()); 11 // 獲取內部存儲空間的緩存目錄 12 File file3 = getCacheDir(); 13 Log.d("fanfan", "內部存儲緩存目錄----" + file3.getPath()); 14 } 通過API獲取路徑?
轉載于:https://www.cnblogs.com/Renyi-Fan/p/7438753.html
總結
以上是生活随笔為你收集整理的获取android的SDK或者手机目录路径的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot实战pdf
- 下一篇: Stanford CoreNLP使用需要