[Android SQLite]数据存储与访问 - 外部存储
接下来就到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;
- }
- }
- }
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20