接下來就到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卡了...具體原因,未知 哈哈
- public class SD_Demo extends Activity implements OnClickListener
- {
- private StringBuilder randomNumBersString = null;
-
- private TextView displayView = null;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main1);
- setupViews();
- }
-
- private void setupViews()
- {
- displayView = (TextView) findViewById(R.id.display);
- findViewById(R.id.random).setOnClickListener(this);
- findViewById(R.id.write).setOnClickListener(this);
- randomNumBersString = new StringBuilder();
- }
-
- @Override
- public void onClick(View v)
- {
- switch (v.getId())
- {
- case R.id.random:
- for (int i = 0; i < 10; i++)
- {
- randomNumBersString.append(Math.random()+"\n");
- }
- displayView.setText(randomNumBersString.toString());
- break;
-
- case R.id.write:
- String fileName = "SdcardFile-"+System.currentTimeMillis()+".txt";;
- boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
- File dir = null;
- if (sdCardExist)
- {
- dir = new File( Environment.getExternalStorageDirectory().toString() + File.separator + "123321");
- if (!dir.exists())
- {
- dir.mkdirs();
- }
- }
- if (dir.exists() && dir.canWrite())
- {
- File newFile = new File(dir.getAbsolutePath()+ "/" + fileName);
- FileOutputStream fos = null;
- try
- {
- newFile.createNewFile();
- if (newFile.exists() && newFile.canWrite())
- {
- fos = new FileOutputStream(newFile);
- fos.write(randomNumBersString.toString().getBytes());
- displayView.setText(String.format("%s文件寫入SD卡", fileName)) ;
- }
- } catch (IOException e)
- {
- e.printStackTrace();
- }finally
- {
- if (fos != null)
- {
- try
- {
- fos.flush();
- fos.close();
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- break;
- }
- }
- }
很有價值的參考文章 http://www.linuxidc.com/Linux/2011-02/32603.htm