歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

[Android SQLite]數據存儲與訪問 - 外部存儲

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

不是所有的手機都有SD卡,但是Android系統本身提供了對SD卡很便捷的訪問方法

相關閱讀:

[Android SQLite]數據存儲與訪問 - 內部存儲 http://www.linuxidc.com/Linux/2011-12/50130.htm

[Android SQLite]數據存儲與訪問 - SharedPreferences http://www.linuxidc.com/Linux/2011-12/50131.htm

一般下載的數據都比較大就都放到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
Copyright © Linux教程網 All Rights Reserved