[置顶] Android学习系列-把文件保存到SD卡上面(6)
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根据URL下载文件保存到SD卡
//下载具体操作 private void download() { try { URL url = new URL(downloadUrl); //打开连接 URLConnection conn = ...
- Android 将文件保存到SD卡,从卡中取文件,及删除文件
//保存到SD卡 private static String sdState = Environment.getExternalStorageState(); private static S ...
- Android 将文件保存到SD卡中
①写文件到sd卡中需要获得权限,在AndroidManifest.xml中添加如下权限: <uses-permission android:name="android.permissi ...
- [置顶] Android学习系列-Android中解析xml(7)
Android学习系列-Android中解析xml(7) 一,概述 1,一个是DOM,它是生成一个树,有了树以后你搜索.查找都可以做. 2,另一种是基于流的,就是解析器从头到尾解析一遍xml文件. ...
- Android开发调试日志工具类[支持保存到SD卡]
直接上代码: package com.example.callstatus; import java.io.File; import java.io.FileWriter; import java.i ...
- Android—将Bitmap图片保存到SD卡目录下或者指定目录
直接上代码就不废话啦 一:保存到SD卡下 File file = new File(Environment.getExternalStorageDirectory(), System.currentT ...
- Android相机、相册获取图片显示并保存到SD卡
Android相机.相册获取图片显示并保存到SD卡 [复制链接] 电梯直达 楼主 发表于 2013-3-13 19:51:43 | 只看该作者 |只看大图 本帖最后由 happy小妖同学 ...
- Libgdx实现异步加载网络图片并保存到SD卡或者data/data目录下边
Libgdx实现异步加载网络图片并保存到SD卡或者data/data目录下边,当本地有图片的时候,直接从本地读取图片,如果本地没有图片,将从服务器异步加载图片 package com.example. ...
- Linux笔记(开机自动将kerne log保存到SD卡中)
有时候为了测试机器的稳定性,需要煲机测试几天的情况,这个时候机器已经封装好,不能再接串口线出来. 为了追溯问题,就需要将log信息保存下来. 于是就需要这样一个功能:系统启动后,自动将kernel的l ...
随机推荐
- UIView添加支持代码块的手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(a ...
- 第25讲 UI组件之 AlertDialog 的各种实现
第25讲 UI组件之AlertDialog 的各种实现 对话框(Dialog)是程序运行中的弹出窗口,例如当用户要删除一个联系方式时,会弹出一个对话框. Android提供了多种对话框:警告对话框(A ...
- Map的内容按字母顺序排序
map有自带的排序功能,但需要重写排序方法,代码如下: package coreJava.com.shindo.corejava.map; import java.util.ArrayList; im ...
- Masters of Doom
http://blog.codinghorror.com/you-dont-need-millions-of-dollars/ "In the information age, the ba ...
- POJ 1001 Exponentiation 模拟小数幂
模拟小数幂 小数点位 pos 非零末位 e 长度 len 只有三种情况 pos > len pos < e e < pos < len #include <iostrea ...
- Javascript兼容收集
1.IE6背景缓存 try{ document.execCommand("BackgroundImageCache", false, true); }catch(e){} 2. e ...
- php解析json数组(循环输出数据)的实例
以快递100接口为例 返回的JSON数据 {"message":"ok","nu":"350116805826",&qu ...
- DataTable 导出Excel 下载 (NPOI)
public class ExcelHelper { public void DownLoadExcelNew(System.Data.DataTable data, Hashtable h, ...
- Service Lane
Link https://www.hackerrank.com/challenges/service-lane def main(): n, t = map(int, raw_input().spli ...
- 爬虫框架YayCrawler
爬虫框架YayCrawler 各位好!从今天起,我将用几个篇幅的文字向大家介绍一下我的一个开源作品——YayCrawler,其在GitHub上的网址是:https://github.com/liush ...