Android学习系列-把文件保存到SD卡上面(6)
来源:互联网 发布:java培训骗局 编辑:程序博客网 时间:2024/04/20 16:02
Android学习系列-把文件保存到SD卡上面(5)
一般多媒体文件,大文件需要保存到SD卡中。关键点如下:
1,SD卡保存目录:mnt/sdcard,一般采用Environment.getExternalStorageDirectory()方式。
public void saveToSDCard(String filename, String content)throws Exception {
File file = new File(Environment.getExternalStorageDirectory(), filename);//指定文件存储目录为SD卡,文件名
FileOutputStream outStream = new FileOutputStream(file);//输出文件流
outStream.write(content.getBytes());
outStream.close();
}
2,需要申请权限,AndroidManifest.xml中配置权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.files"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner" />
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="cn.itcast.files" android:label="Tests for My App" />
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
3.判断SD卡是否存在:Environment.getExternalStorageState().(getApplicationContext());
代码示例:
try {
//判断SDCard是否存在,并且可以读写
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
service.saveToSDCard(filename, content);
Toast.makeText(getApplicationContext(), R.string.success, 1).show();
}else{
Toast.makeText(getApplicationContext(), R.string.sdcarderror, 1).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), R.string.fail, 1).show();
e.printStackTrace();
}
4.总结
关键点:指定SD卡存放文件目录,判断sd卡是否存在,配置SDK访问权限。
一般是将大文件存储到SD卡上。
- Android学习系列-把文件保存到SD卡上面(6)
- android 将文件保存到sd卡上面(简单)
- android 将文件 保存到sd卡 上面
- android 文件保存到SD卡
- android保存文件到SD卡中
- android 文件保存到SD卡
- android保存文件到SD卡中
- android客户端把SD卡文件上传到服务器端并保存在PC硬盘文件夹中
- Android学习笔记(5)——保存文件到sd卡
- Android把log保存到SD卡便于调试
- Android把log保存到SD卡方便调试
- Android 把 log 保存到 SD卡方便调试
- Android 把 log 保存到 SD卡方便调试
- android把文件保存在SD卡上
- android开发学习(二)——保存文件到手机内存和SD卡,文件访问权限,获取内存大小
- android保存文件到SD卡【文件存储】
- Android保存图片到图库,Android扫描文件到媒体库,Android保存图片到SD卡
- Android保存图片到图库,Android扫描文件到媒体库,Android保存图片到SD卡
- TreeView
- ligh oj 1004 - Monkey Banana Problem
- hdu 2254 奥运
- C++模板类的继承1 :模板类继承模板类
- API相关工作的个人总结_Sandcastle简要使用介绍
- Android学习系列-把文件保存到SD卡上面(6)
- HDU4514(非连通图的环判断与图中最长链)
- one of the key features of distributed application-managemeability
- opencv之绘图
- [IOS]包含增删改查移动的tableView展示+plist文件保存+程序意外退出保存Demo
- wikioi p1169 传纸条
- PHP 文件上传后台处理脚本
- POJ 3126 / Northwestern Europe 2006 Prime Path (数论&BFS)
- Android Activity Manifest 标签