
直接上代码:
package com.example.test;import java.io.file;import java.io.fileNotFoundException;import java.io.fileOutputStream;import java.io.IOException;import androID.os.Bundle;import androID.os.Environment;import androID.app.Activity;import androID.Widget.Toast;public class MainActivity extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); //要保存的文件名和文件内容 String filename = "test.txt"; String content = "This is a test."; //判断sdcard是否存在 String state = Environment.getExternalStorageState(); if(state.equals(Environment.MEDIA_MOUNTED)) { //获取SDCard目录 file sdcardpath = Environment.getExternalStorageDirectory(); file file = new file(sdcardpath,filename); fileOutputStream fos; try { fos = new fileOutputStream(file); fos.write(content.getBytes()); fos.close(); } catch (fileNotFoundException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this,"sdcard不存在获取不可写入",Toast.LENGTH_SHORT).show(); } }}总结
以上是内存溢出为你收集整理的android中把文件保存到sdcard代码实例全部内容,希望文章能够帮你解决android中把文件保存到sdcard代码实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)