外部存储的特点:
1,外部存储不一定是有效的
2,数据存储是全局可读的 可以被别的程序读取
MainActivity:
1,外部存储不一定是有效的
2,数据存储是全局可读的 可以被别的程序读取
3,当删除本应用程序 内部私有的数据会被删除
代码:
Utils:
package com.qianfeng.extranalstorage.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.content.Context;
import android.os.Environment;
public class SdCardUtils {
/**
* 他是用来判断外部设备存储是否可用
*
* @return
*/
public static boolean isExtraStorage() {
boolean bl = false;
// 获取外部存储状态 与外部可读写状态比较 返回值 相等 true 不相等是false
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
bl = true;
}
return bl;
}
/**
* 写入一个外部存储(公共)
*
* @return
*/
public static boolean WriteStoragePublic(Context context, String fileName,
String content) {
boolean isStorage = false;
// 创建外部存储文件 Environment.DIRECTORY_DOWNLOADS
// 类型必须指定 否则报错
// 存储路径 mnt/sdcard/Download/fileName
File file = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
fileName);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
outputStream.write(content.getBytes());
isStorage = true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return isStorage;
}
/**
* 读取数据(公共的)
* @param fileName
* @return
*/
public static String ReadStoragePublic(String fileName) {
boolean b1 = false;
StringBuffer stringBuffer = new StringBuffer();
File file = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
fileName);
FileInputStream inputStream=null;
BufferedReader br=null;
try {
inputStream = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(inputStream));
String line =null;
while((line=br.readLine())!=null){
stringBuffer.append(line);
}
return stringBuffer.toString();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
/**
* 写入一个外部存储(私有)
*
* @return
*/
public static boolean WriteStoragePrivate(Context context, String fileName,
String content) {
boolean isStorage = false;
File file = new File(
context.getExternalFilesDir(null),
fileName);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
outputStream.write(content.getBytes());
isStorage = true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return isStorage;
}
/**
* 读取数据(私有的)
* @param fileName
* @return
* 存储路径:mmnt/Android/data/包名/files/text.txt
*/
public static String ReadStoragePrivate(Context context,String fileName) {
boolean b1 = false;
StringBuffer stringBuffer = new StringBuffer();
File file = new File(
context.getExternalFilesDir(null),
fileName);
FileInputStream inputStream=null;
BufferedReader br=null;
try {
inputStream = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(inputStream));
String line =null;
while((line=br.readLine())!=null){
stringBuffer.append(line);
}
return stringBuffer.toString();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
/**
* 获取文件字节总长度
* getExternalStorageDirectory() sdcard的根目录
* @return
*/
public static long GetLenth(){
File file = new File(Environment.getExternalStorageDirectory(), "");
long l =file.getTotalSpace();
return l;
}
/**
* 获取文件对象可以使用的字节长度
* getExternalStorageDirectory() sdcard的根目录
* @return
*/
public static long GetFreeLenth(){
File file = new File(Environment.getExternalStorageDirectory(), "");
long l =file.getFreeSpace();
return l;
}
/**
* 删除指定的文件
* getExternalStorageDirectory() sdcard的根目录
* @return
*/
public static boolean DeleteData(String fileName){
// File file = new File(Environment.getExternalStorageDirectory(),fileName);
File file = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
fileName);
boolean b = file.delete();
return b;
}
}
MainActivity:
package com.qianfeng.extranalstorage;
import com.qianfeng.extranalstorage.utils.SdCardUtils;
import android.R.bool;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
private final static String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onclickBtn(View v){
switch (v.getId()) {
case R.id.button1://检测外部设备是否可用
boolean isAble= SdCardUtils.isExtraStorage();
if(isAble){
Log.i(TAG, "外部设备是可用");
}else{
Log.i(TAG, "外部设备是可用");
}
break;
case R.id.button2:
boolean b2 =SdCardUtils.WriteStoragePublic(MainActivity.this, "info.txt", "asdfghjkl");
if(b2){
Log.i(TAG, "写入到外部存储有效");
}else{
Log.i(TAG, "写入到外部存储无效");
}
break;
case R.id.button3:
String str = SdCardUtils.ReadStoragePublic("info.txt");
Log.i(TAG, str);
break;
case R.id.button4:
boolean b4 =SdCardUtils.WriteStoragePrivate(MainActivity.this, "text.txt", "好好学习");
if(b4){
Log.i(TAG, "444写入到外部存储有效");
}else{
Log.i(TAG, "444写入到外部存储无效");
}
break;
case R.id.button5:
String str1 = SdCardUtils.ReadStoragePrivate(MainActivity.this,"text.txt");
Log.i(TAG, str1);
break;
case R.id.button6://获取指定文件的总字节长度
long l = SdCardUtils.GetLenth();
Log.i(TAG, "获取指定文件的总字节长度"+l);
break;
case R.id.button7://获取指定文件的剩余字节长度
long l1 = SdCardUtils.GetFreeLenth();
Log.i(TAG, "获取指定文件的剩余字节长度"+l1);
break;
case R.id.button8://删除指定的文件
boolean b8 = SdCardUtils.DeleteData("info.txt");
if(b8){
Log.i(TAG, "删除指定的文件成功");
}else{
Log.i(TAG, "删除指定的文件失败");
}
break;
default:
break;
}
}
}
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qianfeng.extranalstorage"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<!-- 加读和写的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.qianfeng.extranalstorage.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>
</manifest>