接下来就到SD卡了吧 ^_^
不是所有的手机都有SD卡,但是Android系统本身提供了对SD卡很便捷的访问方法
相关阅读:
一般下载的数据都比较大就都放到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