Chinaunix首页 | 论坛 | 博客
  • 博客访问: 478282
  • 博文数量: 47
  • 博客积分: 2044
  • 博客等级: 上尉
  • 技术积分: 400
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-11 15:50
文章分类

全部博文(47)

文章存档

2012年(1)

2011年(19)

2010年(27)

我的朋友

分类: 嵌入式

2010-11-01 11:32:19

将文件存在sd卡中和存在手机中的思路相同就是路径不同,注意看类中的到sd卡路径的注释
Java代码 复制代码
  1. package cn.lee.data;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileInputStream;   
  5. import java.io.FileOutputStream;   
  6. import java.io.IOException;   
  7.   
  8. import cn.lee.Manager.DataManager;   
  9. import android.app.Activity;   
  10. import android.content.Context;   
  11. import android.os.Bundle;   
  12. import android.os.Environment;   
  13. import android.util.Log;   
  14. import android.view.View;   
  15. import android.view.View.OnClickListener;   
  16. import android.widget.Button;   
  17. import android.widget.EditText;   
  18. import android.widget.Toast;   
  19.   
  20. public class AboutDateActivity extends Activity {   
  21.     private EditText fileNameEditText;   
  22.     private EditText fileContentEditText;   
  23.     private EditText readfileContentEditText;   
  24.     private Button button;   
  25.     private Button readButton;   
  26.     private static final String TAGSTRING = "AboutDateActivity";   
  27.   
  28.     /** Called when the activity is first created. */  
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {   
  31.         super.onCreate(savedInstanceState);   
  32.         setContentView(R.layout.main);   
  33.   
  34.         button = (Button) this.findViewById(R.id.button);   
  35.         readButton = (Button) this.findViewById(R.id.readButton);   
  36.         fileNameEditText = (EditText) this.findViewById(R.id.fileName);   
  37.         fileContentEditText = (EditText) this.findViewById(R.id.fileContent);   
  38.         readfileContentEditText = (EditText) this  
  39.                 .findViewById(R.id.readfileContent);   
  40.   
  41.         // 读取   
  42.         readButton.setOnClickListener(onClickListener);   
  43.         // 保存   
  44.         button.setOnClickListener(onClickListener);   
  45.     }   
  46.   
  47.     /**  
  48.      * 由于这个内部实现类是一个接口的实例 所以这里把他提取出来 作为一个成员变量 2010-04-18  
  49.      */  
  50.     private View.OnClickListener onClickListener = new View.OnClickListener() {   
  51.         /**  
  52.          * 由于button的父类是view 所以这里的onclick的参数传来的view v就可以找到这个button  
  53.          */  
  54.         public void onClick(View v) {   
  55.             Button button = (Button) v;// 将父类view强转成子类button   
  56.             int info = R.string.sus;   
  57.             String fileNameString = fileNameEditText.getText().toString();   
  58.                
  59.             /**  
  60.              * 判断sd卡是否存在 Environment.getExternalStorageState() 得到sd卡当前的状态  
  61.              *   
  62.              * getExternalStorageState() returns MEDIA_MOUNTED if the  
  63.              * media is present and mounted at its mount point with  
  64.              * read/write access. 如果返回 MEDIA_MOUNTED表示外部存储设备存在。并且有读写的权限(因为sd卡有写保护 如果写保护关闭也是没有权限读写的)  
  65.              */  
  66.                
  67.             if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {   
  68.                 if ("".equals(fileNameString)) {   
  69.                     info = R.string.sdNotExit;   
  70.                 } else {   
  71.                     FileOutputStream fileOutputStream = null;   
  72.                     FileInputStream fileInputStream = null;   
  73.                     try {   
  74.                         /**  
  75.                          * Environment.getExternalStorageDirectory();  
  76.                          * 得到外存储设备的路径  
  77.                          *   
  78.                          */  
  79.                         File file = new File(Environment.getExternalStorageDirectory(),fileNameString);   
  80.                            
  81.                         switch (button.getId()) {   
  82.                         case R.id.button: {// 保存   
  83.                             String fileContentString = fileContentEditText.getText().toString();   
  84.                                
  85.                             fileOutputStream = new FileOutputStream(file);   
  86.                             DataManager.saveDate(fileOutputStream,fileContentString);   
  87.                             break;   
  88.                         }   
  89.   
  90.                         case R.id.readButton: {//读取   
  91.                             fileInputStream = new FileInputStream(file);   
  92.                             String contentString = DataManager.readDate(fileInputStream);   
  93.                             readfileContentEditText.setText(contentString);   
  94.                             break;   
  95.                         }   
  96.                         default:   
  97.                             break;   
  98.                         }   
  99.   
  100.                     } catch (Exception e) {   
  101.                         // TODO Auto-generated catch block   
  102.                         Log.i(TAGSTRING, e.toString());   
  103.                         info = R.string.infor;   
  104.                     } finally {   
  105.                         try {   
  106.                             if (fileOutputStream != null) {   
  107.                                 fileOutputStream.close();   
  108.                             }   
  109.                             if (fileInputStream != null) {   
  110.                                 fileInputStream.close();   
  111.                             }   
  112.   
  113.                         } catch (IOException e) {   
  114.   
  115.                             Log.i(TAGSTRING, e.toString());   
  116.                             info = R.string.infor;   
  117.                         }   
  118.                     }   
  119.   
  120.                 }   
  121.             } else {   
  122.                 info = R.string.infor;   
  123.             }   
  124.             Toast.makeText(AboutDateActivity.this, info, 1).show();   
  125.         }   
  126.   
  127.     };   
  128. }  
阅读(2490) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~