日期16年8月30日学习

一、共享参数:

(1)、概念:

SharedPreferences是Android系统提供的一个通用的数据持久化框架,用于存储和读取key-value类型的原始基本数据类型对,目前支持String、int、long、float、boolean等基本类型的存储,对于自定义的对象数据类型,无法使用SharedPreferences来存储。

SharedPreferences主要用于存储系统的配置信息。例如上次登录的用户名,上次最后设置的配置信息(如:是否打开音效、是否使用振动,小游戏的玩家积分等)。当再次启动程序后依然保持原有设置。SharedPreferences用键值对方式存储,方便写入和读取。

(2)、常用方法:

/**
* --------------------------------保存共享参数--------------
*/
//        /*
//         * 1、通过context对象获得共享参数的对象
//         * 参数一:文件名称
//         * 参数二:指定共享参数文件的模式
//         *         Context.MODE_PRIVATE -> 表示该共享参数文件只能被本应用使用
//         *
//         *         4.2.2以后过时
//         *         Context.MODE_WORLD_READABLE -> 该共享参数文件可以被其他应用读取,以只读的方式打开
//         *         Context.MODE_WORLD_WRITEABLE -> 其他应用可以读写该共享参数文件
//         */
//        sharedPreferences = this.getSharedPreferences("config", Context.MODE_PRIVATE);
//        /*
//         * 2、通过共享参数的对象,再获得共享参数的编辑对象
//         */
//        editor = sharedPreferences.edit();
//
//        /*
//         * 3、用编辑对象填写内容
//         */
//        editor.putString("name", "张三");
//        editor.putInt("age", 16);
//
//        /*
//         * 4、提交
//         */
//        editor.commit();
-------------读取共享参数---------------

同样是先创建共享参数,指定共享模式

sharedPreferences = this.getSharedPreferences("config", Context.MODE_PRIVATE);

获取key值为name的String参数放到String中。也可以获得int类型的,把String改为int即可

String name = sharedPreferences.getString("name", null);

---共享参数可以用来保存用户名和密码,它是存储在内存中,在手机上要访问就要获得root权限。当然也有其他用处

--------------------------------下面创建一个共享参数的工具类------------------------
/**
* 共享参数的工具类
*
*/
public class SharedUtil {
private static SharedPreferences sharedPreferences;
private static SharedPreferences.Editor editor;
/**
* 共享参数初始化
*/
public static void init(Context context){
sharedPreferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
}
public static void putString(String key, String value){
//putString是将一个String的参数存入里面
editor.putString(key, value);
editor.commit();
}
public static String getString(String key){
//getString是获取参数
return sharedPreferences.getString(key, null);
}
public static void putInt(String key, int value){
editor.putInt(key, value);
editor.commit();
}
public static int getInt(String key){
return sharedPreferences.getInt(key, -1);
}
public static void putBoolean(String key, boolean value){
editor.putBoolean(key, value);
editor.commit();
}
public static boolean getBoolean(String key){
return sharedPreferences.getBoolean(key, false);
}
}

二、内部存储

---------

一般内部文件存储的路径为:

/data/data/{appPackageName}/files/{filename}

内部存储都是用io流来读和写、操作方式和io流没有太多差别

----------获得内部存储的文件---使用for循环遍历输出----------

String[] strs = this.fileList();
for(String str : strs){
Log.d("print", "内部存储中的文件:" + str);
}
* 获得输出流----------将数据写到test.txt的文件中
* Context.MODE_APPEND 追加
Context.MODE_PRIVATE  不追加(覆盖)
*/
//        FileOutputStream out = null;
//        try {
//            out = this.openFileOutput("test.txt", Context.MODE_PRIVATE);
//            out.write("大王叫我来巡山".getBytes());
//            out.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
/*
* 获得输入流-----------将test.txt文件中的数据读出来输出到log上
*/
//        FileInputStream in = null;
//        try {
//            in = this.openFileInput("test.txt");
//            byte[] bs = new byte[1024];
//            in.read(bs);
//
//            String str = new String(bs).trim();
//            in.close();
//            Log.d("print", "文件中的内容:" + str);
----------------删除内部存储中的文件----------------
Context.deleteFile(String Filename)

三、拓展存储

----外部存储---------------
--------------------读写SD卡步骤-------------------------
public class SDCardHelper {
private static String TAG = "SDCardHelper";
/*
* 判断sdcard是否挂载
*/
public static boolean isSDCardMounted() {
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
}
/*
* 获取sdcard绝对物理路径
*/
public static String getSDCardPath() {
if (isSDCardMounted()) {
return Environment.getExternalStorageDirectory().getAbsolutePath();
} else {
return null;
}
}
/*
* 获取sdcard的全部的空间大小。返回MB字节
*/
public static long getSDCardSize() {
if (isSDCardMounted()) {
StatFs fs = new StatFs(getSDCardPath());
long size = fs.getBlockSize();
long count = fs.getBlockCount();
return size * count / 1024 / 1024;
}
return 0;
}
/*
* 获取sdcard的剩余的可用空间的大小。返回MB字节
*/
public static long getSDCardFreeSize() {
if (isSDCardMounted()) {
StatFs fs = new StatFs(getSDCardPath());
long size = fs.getBlockSize();
long count = fs.getAvailableBlocks();
return size * count / 1024 / 1024;
}
return 0;
}
/*
* 将文件(byte[])保存进sdcard指定的路径下
*/
public static boolean saveFileToSDCard(byte[] data, String dir,
String filename) {
BufferedOutputStream bos = null;
if (isSDCardMounted()) {
Log.i(TAG, "==isSDCardMounted==TRUE");
File file = new File(getSDCardPath() + File.separator + dir);
Log.i(TAG, "==file:" + file.toString() + file.exists());
if (!file.exists()) {
boolean flags = file.mkdirs();
Log.i(TAG, "==创建文件夹:" + flags);
}
try {
bos = new BufferedOutputStream(new FileOutputStream(new File(
file, filename)));
bos.write(data, 0, data.length);
bos.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
/*
* 已知文件的路径,从sdcard中获取到该文件,返回byte[]
*/
public static byte[] loadFileFromSDCard(String filepath) {
BufferedInputStream bis = null;
ByteArrayOutputStream baos = null;
if (isSDCardMounted()) {
File file = new File(filepath);
if (file.exists()) {
try {
baos = new ByteArrayOutputStream();
bis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024 * 8];
int c = 0;
while ((c = bis.read(buffer)) != -1) {
baos.write(buffer, 0, c);
baos.flush();
}
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return null;
}
}-

-------------------------------------------------------

//判断SD卡是否存在
//        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//            Log.d("print", "---->SD卡正常加载");
//        } else {
//            Log.d("print", "---->没有SD卡");
//        }
//获得拓展卡路径
//        File file = Environment.getExternalStorageDirectory();
//        Log.d("print", "外部存储的路径----->" + file.getAbsolutePath());
//        File[] files = file.listFiles();
//        for(File f : files){
//            Log.d("print", "拓展卡下的文件:" + f.getAbsolutePath());
//        }
//往拓展卡中存放一个小机器人
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//拓展卡存在
// /xxx/xxx/xxx/jqr.png
File file = new File(Environment.getExternalStorageDirectory(), "jqr.png");
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
try {
bitmap.compress(CompressFormat.PNG, 100, new FileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

------------------------读写拓展存储还需要获取权限--------------

读权限:android.permission.READ_EXTERNAL_STOARAGE

读写权限:android.permission.WRITE_EXTERNAL_STOARAGE