[Android SQLite]数据存储与访问 - 外部存储

接下来就到SD卡了吧 ^_^

不是所有的手机都有SD卡,但是Android系统本身提供了对SD卡很便捷的访问方法

相关阅读:

一般下载的数据都比较大就都放到SD卡了...具体原因,未知 哈哈

  1. public class SD_Demo extends Activity implements OnClickListener  
  2. {  
  3.     private StringBuilder randomNumBersString = null;  
  4.       
  5.     private TextView displayView = null;  
  6.       
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState)  
  9.     {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main1);  
  12.         setupViews();  
  13.     }  
  14.   
  15.     private void setupViews()  
  16.     {  
  17.         displayView = (TextView) findViewById(R.id.display);  
  18.         findViewById(R.id.random).setOnClickListener(this);  
  19.         findViewById(R.id.write).setOnClickListener(this);  
  20.         randomNumBersString = new StringBuilder();  
  21.     }  
  22.   
  23.     @Override  
  24.     public void onClick(View v)  
  25.     {  
  26.         switch (v.getId())  
  27.         {  
  28.             case R.id.random:  
  29.                 for (int i = 0; i < 10; i++)  
  30.                 {  
  31.                     randomNumBersString.append(Math.random()+"\n");  
  32.                 }  
  33.                 displayView.setText(randomNumBersString.toString());  
  34.                 break;  
  35.                   
  36.             case R.id.write:  
  37.                 String fileName = "SdcardFile-"+System.currentTimeMillis()+".txt";;  
  38.                 boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);   
  39.                 File dir = null;  
  40.                 if (sdCardExist)  
  41.                 {  
  42.                     dir = new File( Environment.getExternalStorageDirectory().toString() + File.separator + "123321");  
  43.                     if (!dir.exists())  
  44.                     {  
  45.                         dir.mkdirs();  
  46.                     }  
  47.                 }  
  48.                 if (dir.exists() && dir.canWrite())  
  49.                 {  
  50.                     File newFile = new File(dir.getAbsolutePath()+ "/" + fileName);  
  51.                     FileOutputStream fos = null;  
  52.                     try  
  53.                     {  
  54.                         newFile.createNewFile();  
  55.                         if (newFile.exists() && newFile.canWrite())  
  56.                         {  
  57.                             fos = new FileOutputStream(newFile);  
  58.                             fos.write(randomNumBersString.toString().getBytes());  
  59.                             displayView.setText(String.format("%s文件写入SD卡", fileName))   ;  
  60.                         }  
  61.                     } catch (IOException e)  
  62.                     {  
  63.                         e.printStackTrace();  
  64.                     }finally  
  65.                     {  
  66.                         if (fos != null)  
  67.                         {  
  68.                             try  
  69.                             {  
  70.                                 fos.flush();  
  71.                                 fos.close();  
  72.                             } catch (IOException e)  
  73.                             {  
  74.                                 e.printStackTrace();  
  75.                             }  
  76.                         }  
  77.                     }  
  78.                 }  
  79.                 break;  
  80.         }  
  81.     }  
  82. }  
很有价值的参考文章 http://www.linuxidc.com/Linux/2011-02/32603.htm

相关推荐