android sd卡相关,Android-SD卡相关操作

本文介绍了如何在Android应用中操作SD卡,包括获取文件路径、检查存储状态、计算空间大小,以及使用File和Content API进行文本文件的读写。重点展示了文件I/O操作和将图片保存到相册的方法,并提供实例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SD卡相关操作

1、获取 App 文件目录

//获取 当前APP 文件路径

String path1 = this.getFilesDir().getPath();

当前APP目录也就是应用的这个目录 /data/data/com.tiger.helloworld/files

4e18c8da33b26cb5d60d7eb9972c46fb.png

2、获取外部存储器 路径

一般手机文件管理 根路径 /storage/emulated/0/

//获取外部存储器 路径

String path2= Environment.getExternalStorageDirectory().getPath();

3、判断SD卡是否可用

if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))

4、获取SD总大小,可用空间

// 获取SD卡的总大小,可用空间

File file= Environment.getExternalStorageDirectory();

long totalSpace= file.getTotalSpace(); //总大小

long usableSpace= file.getUsableSpace(); //可用空间

//转换数据格式

String formatTotalSpace= Formatter.formatFileSize(MainActivity.this,totalSpace);

String formatUsableSpace=Formatter.formatFileSize(MainActivity.this,usableSpace);

Toast.makeText(MainActivity.this, "Total Space:"+formatTotalSpace

+" UsableSpace:"+formatUsableSpace, Toast.LENGTH_LONG).show();

5.文本文件读写

Type1:

1、写入数据

try {

String result = userName + "_" + userPwd;

//【1】创建一个File 类 指定我们要把数据存储的位置

File file = new File("/data/data/com.tiger.helloworld/info.txt");

//【2】 创建一个文件输出流

FileOutputStream fos = new FileOutputStream(file);

//【3】写入数据

fos.write(result.getBytes());

fos.close();

return true;

} catch (Exception e) {

Log.e("Utils", e.getMessage());

e.printStackTrace();

return false;

}

2、读取数据

try {

File file = new File("/data/data/com.tiger.helloworld/info.txt");

FileInputStream fis = new FileInputStream(file);

BufferedReader bufr = new BufferedReader(new InputStreamReader(fis));

String userInfo = bufr.readLine();

fis.close();

return userInfo;

} catch (Exception e) {

Log.e("Utils", e.getMessage());

e.printStackTrace();

return null;

}

Type2:

1、通过 Content 写入数据

try {

String result = userName + "_" + userPwd;

FileOutputStream fos = context.openFileOutput("info.txt", context.MODE_PRIVATE);

fos.write(result.getBytes());

fos.close();

return true;

} catch (Exception e) {

Log.e("Utils", e.getMessage());

e.printStackTrace();

return false;

}

2、通过 Content 读取数据

try {

FileInputStream fis = context.openFileInput("info.txt");

BufferedReader bufr = new BufferedReader(new InputStreamReader(fis));

String userInfo = bufr.readLine();

/*while (bufr.readLine()!=null){

str+=bufr.readLine();

}*/

fis.close();

return userInfo;

} catch (Exception e) {

Log.e("Utils", e.getMessage());

e.printStackTrace();

return null;

}

6.保存InputStream图片到本地相册中

/**

* 保存图片,并显示到 Gallery 中

*/

public void saveImage(InputStream inputStream){

try {

File externalFile = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

File directoryFile = new File(externalFile.getPath() + "/MY");

//若目录不存在则创建

if (!directoryFile.exists()) {

directoryFile.mkdirs();

}

File imageFile = new File(directoryFile, "qr"+directoryFile.list().length+".jpg");

FileOutputStream fos=new FileOutputStream(imageFile);

byte[] buffer=new byte[1024];

int len=-1;

while ((len=inputStream.read(buffer))!=-1){

fos.write(buffer,0,len);

}

inputStream.close();

fos.close();

//发送广播扫面文件,使图片显示在Gallery中

Intent intent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

intent.setData(Uri.fromFile(imageFile));

mContext.sendBroadcast(intent);

Log.e("","Save successful!");

} catch (Exception e) {

e.printStackTrace();

Log.e("","Save unsuccessful!");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值