获取android的SDK或者手机目录路径

时间:2023-03-09 22:09:21
获取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

 package com.example.readwrite;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log; /**
* 正斜杠代表根目录 两种最常见的数据存储方式
*
* 一、内存 二、本地 1.手机内部存储 2.外部存储设备(SD卡)
* */
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// existSDcard();
// write();
listPath();
//read();
} private void write() {
// /mnt/sdcard
File file = Environment.getExternalStorageDirectory();
FileOutputStream out = null;
try {
out = new FileOutputStream(file.getPath() + "/fanfan.txt");
// out = new FileOutputStream(
// "/data/data/com.example.readwrite/fanfan.txt");
out.write("12345".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} private void read() {
FileInputStream in = null;
try {
// in = new FileInputStream("/mnt/sdcard/fanfan.txt");
in = new FileInputStream(
"/data/data/com.jiguang.test/databases/rep.db");
byte[] bytes = new byte[2014];
int len = in.read(bytes);
String str = new String(bytes, 0, len);
Log.d("fanfan", "---------" + str);
} catch (IOException e) {
Log.d("fanfan","报错啦"+e.toString());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* 检查SD卡是否被挂载
* */
private void existSDcard() {
// 获取SD卡的状态
String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) {
Log.d("fanfan", "有SD卡");
} else {
Log.d("fanfan", "没有SD卡");
}
} /**
* 通过API获取路径
* */
private void listPath() {
// 获取SD卡目录
File file1 = Environment.getExternalStorageDirectory();
Log.d("fanfan", "sd卡----" + file1.getPath());
// 获取手机内部存储空间的file目录
File file2 = getFilesDir();
Log.d("fanfan", "内部存储File----" + file2.getPath());
// 获取内部存储空间的缓存目录
File file3 = getCacheDir();
Log.d("fanfan", "内部存储缓存目录----" + file3.getPath());
}
}

获取路径

     /**
* 检查SD卡是否被挂载
* */
private void existSDcard() {
// 获取SD卡的状态
String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) {
Log.d("fanfan", "有SD卡");
} else {
Log.d("fanfan", "没有SD卡");
}
}

检查SD卡是否被挂载

     /**
* 通过API获取路径
* */
private void listPath() {
// 获取SD卡目录
File file1 = Environment.getExternalStorageDirectory();
Log.d("fanfan", "sd卡----" + file1.getPath());
// 获取手机内部存储空间的file目录
File file2 = getFilesDir();
Log.d("fanfan", "内部存储File----" + file2.getPath());
// 获取内部存储空间的缓存目录
File file3 = getCacheDir();
Log.d("fanfan", "内部存储缓存目录----" + file3.getPath());
}

通过API获取路径