将文件保存到内存、SD以及获取手机内部存储与外部存储空间的大小

来源:互联网 发布:tk 域名 编辑:程序博客网 时间:2024/04/29 17:57

微笑将文件保存到内存(data/data目录下)

 

先介绍一下Context,上下文,就是一个类;提供一些方便的api,可以得到应用程序的环境、环境包名、安装路径、文件的路径、资源的路径、资产的路径。

 

MainActivity.java

[java] view plaincopy
  1. package com.lee.login;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import com.lee.service.MyService;  
  6.   
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.Toast;  
  13. import android.app.Activity;  
  14.   
  15. public class MainActivity extends Activity implements OnClickListener {  
  16.     private EditText editTextName, editTextPassword;  
  17.     private Button login;  
  18.     private MyService myService;  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.         editTextName = (EditText) this.findViewById(R.id.etName);  
  25.         editTextPassword = (EditText) this.findViewById(R.id.etPassword);  
  26.         login = (Button) this.findViewById(R.id.login);  
  27.         //button的点击事件  
  28.         login.setOnClickListener(this);  
  29.           
  30.         //得到map  
  31.         Map<String, String> map = myService.getInfo(this);  
  32.         if (map != null) {  
  33.             editTextName.setText(map.get("name"));  
  34.             editTextPassword.setText(map.get("password"));  
  35.         }  
  36.     }  
  37.   
  38.     public void login() {  
  39.         //得到登陆信息,并保存到起来  
  40.         String name = editTextName.getText().toString().trim();  
  41.         String password = editTextPassword.getText().toString().trim();  
  42.         Toast.makeText(this, name + " : " + password, Toast.LENGTH_SHORT)  
  43.                 .show();  
  44.         myService.saveInfo(this, name, password);  
  45.   
  46.     }  
  47.   
  48.     @Override  
  49.     public void onClick(View v) {  
  50.         // TODO Auto-generated method stub  
  51.         login();  
  52.     }  
  53.   
  54. }  


MyService.java

[java] view plaincopy
  1. package com.lee.service;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.InputStreamReader;  
  8. import java.util.HashMap;  
  9. import java.util.Map;  
  10.   
  11. import android.content.Context;  
  12.   
  13. public class MyService {  
  14.   
  15.     public static boolean saveInfo(Context context, String name, String password) {  
  16.         //通过context得到data/data/files文件夹路径  
  17.         File file = new File(context.getFilesDir(), "login.txt");  
  18.         try {  
  19.             //向文件写入注册信息  
  20.             FileOutputStream fos = new FileOutputStream(file);  
  21.             String str = name + "##" + password;  
  22.             fos.write(str.getBytes());  
  23.         } catch (Exception e) {  
  24.             // TODO Auto-generated catch block  
  25.             return false;  
  26.         }  
  27.         return true;  
  28.     }  
  29.   
  30.     public static Map<String, String> getInfo(Context context) {  
  31.         File file = new File(context.getFilesDir(), "login.txt");  
  32.         //由文件读取信息,并写入map  
  33.         try {  
  34.             BufferedReader br = new BufferedReader(new InputStreamReader(  
  35.                     new FileInputStream(file)));  
  36.             String text = br.readLine();  
  37.             Map<String, String> map = new HashMap<String, String>();  
  38.   
  39.             String[] strs = text.split("##");  
  40.             map.put("name", strs[0]);  
  41.             map.put("password", strs[1]);  
  42.             return map;  
  43.   
  44.         } catch (Exception e) {  
  45.             // TODO Auto-generated catch block  
  46.             return null;  
  47.         }  
  48.     }  
  49. }  

 

 

上面程序中的File file = new File(context.getFilesDir(), "login.txt");可以使用context.openFileOutput("login.txt",mode);替换.

mode:

Context.MODE_PRIVATE 私有

Context.MODE_WORLD_READABLE 只读

Context.MODE_WORLD_WRITEABLE 只写

Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE 读写

 

 

如果考虑将信息保存到内存卡,那程序代码只需改变文件的路径,这里通过Environment得到内存卡的状态,并且判断正常后存入即可,不过,仍要在AndroidManifest.xml中加入

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

修改后的代码

[java] view plaincopy
  1. public static boolean saveInfo(Context context, String name, String password) {  
  2.         // 通过Environment得到SD的状态和路径  
  3.         if (Environment.getExternalStorageState().equals(  
  4.                 Environment.MEDIA_MOUNTED)) {  
  5.             File file = new File(Environment.getExternalStorageDirectory(),  
  6.                     "login.txt");  
  7.             try {  
  8.                 // 向文件写入注册信息  
  9.                 FileOutputStream fos = new FileOutputStream(file);  
  10.                 String str = name + "##" + password;  
  11.                 fos.write(str.getBytes());  
  12.             } catch (Exception e) {  
  13.                 // TODO Auto-generated catch block  
  14.                 return false;  
  15.             }  
  16.         }  
  17.         return true;  
  18.     }  
  19.   
  20.     public static Map<String, String> getInfo(Context context) {  
  21.         // 通过Environment得到SD的状态和路径  
  22.         if (Environment.getExternalStorageState().equals(  
  23.                 Environment.MEDIA_MOUNTED)) {  
  24.             File file = new File(Environment.getExternalStorageDirectory(),  
  25.                     "login.txt");  
  26.             // 由文件读取信息,并写入map  
  27.             try {  
  28.                 BufferedReader br = new BufferedReader(new InputStreamReader(  
  29.                         new FileInputStream(file)));  
  30.                 String text = br.readLine();  
  31.                 Map<String, String> map = new HashMap<String, String>();  
  32.   
  33.                 String[] strs = text.split("##");  
  34.                 map.put("name", strs[0]);  
  35.                 map.put("password", strs[1]);  
  36.                 return map;  
  37.   
  38.             } catch (Exception e) {  
  39.                 // TODO Auto-generated catch block  
  40.                 return null;  
  41.             }  
  42.         }  
  43.         return null;  
  44.     }  

 

 

微笑获取手机内部和外部存储空间的大小

 

步骤:

1.得到所要查询的存储空间的路径

  File file = Environment.getExternalStorageDirectory();

2.根据路径,得到StatFs状态

  StatFs stat = new StatFs(file.getPath());

3.根据StatFs状态得到各个属性,包括各种存储块与存储块大小

  long availableBlocks = stat.getAvailableBlocks();
  long blockCount = stat.getBlockCount();
  long blockSize = stat.getBlockSize();
  long freeBlocks = stat.getFreeBlocks();

4.对查询到的数字,按照字节大小的特点进行格式化。

  Formatter formatter = new Formatter();
  String total = formatter.formatFileSize(this, blockSize*blockCount);
  String free = formatter.formatFileSize(this, blockSize*freeBlocks);

代码:

[java] view plaincopy
  1. package com.lee.rom;  
  2.   
  3. import java.io.File;  
  4.   
  5. import android.os.Bundle;  
  6. import android.os.Environment;  
  7. import android.os.StatFs;  
  8. import android.app.Activity;  
  9. import android.text.format.Formatter;  
  10. import android.widget.TextView;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     private TextView textView;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.   
  21.         textView = (TextView) this.findViewById(R.id.tv);  
  22.         textView.setText(this.getSdInfo()+"\n"+this.getRomInfo());  
  23.     }  
  24.       
  25.     /** 
  26.      * 得带sd的存储空间信息 
  27.      * @return 
  28.      */  
  29.     public String getSdInfo() {  
  30.         //此处为data/data路径下的存储空间  
  31.         File file = Environment.getExternalStorageDirectory();  
  32.         //得到路径下的存储空间的状态,根据此状态得到各种属性  
  33.         StatFs stat = new StatFs(file.getPath());  
  34.         long availableBlocks = stat.getAvailableBlocks();  
  35.         long blockCount = stat.getBlockCount();  
  36.         long blockSize = stat.getBlockSize();  
  37.         long freeBlocks = stat.getFreeBlocks();  
  38.         Formatter formatter = new Formatter();  
  39.         //格式化,将数字转化为存储空间的字节大小  
  40.         String total = formatter.formatFileSize(this, blockSize*blockCount);  
  41.         String free = formatter.formatFileSize(this, blockSize*freeBlocks);  
  42.         return "SD总空间:"+(total)+" 可用空间:"+(free);  
  43.     }  
  44.   
  45.     /** 
  46.      * 得到Rom的存储信息 
  47.      * @return 
  48.      */  
  49.     public String getRomInfo() {  
  50.         //此处为SDCard的存储空间  
  51.         File file = Environment.getDataDirectory();  
  52.         StatFs stat = new StatFs(file.getPath());  
  53.         long availableBlocks = stat.getAvailableBlocks();  
  54.         long blockCount = stat.getBlockCount();  
  55.         long blockSize = stat.getBlockSize();  
  56.         long freeBlocks = stat.getFreeBlocks();  
  57.         Formatter formatter = new Formatter();  
  58.         String total = formatter.formatFileSize(this, blockSize*blockCount);  
  59.         String free = formatter.formatFileSize(this, blockSize*freeBlocks);  
  60.         return "ROM总空间:"+(total)+" 可用空间:"+(free);  
  61.     }  
  62.   
  63. }  

运行结果:


转自:http://blog.csdn.net/helloxiaobi/article/details/12293197

0 0