设为首页 加入收藏

TOP

获取android的SDK或者手机目录路径(一)
2017-10-11 15:34:12 】 浏览:10139
Tags:获取 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     }
检查S
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇在android中读写文件 下一篇Json解析与Gson解析

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目