The first Activity in FileExplorer is a screen that allows users to choose whether towork with the internal or external storage.

If the user chooses the internal storagepath, we then go to an Activity named InternalStorage.

the user enters sometext and clicks the Write button to store that text to a file. When they click Read, thefile is read back and displayed.

create layout file main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView     android:id="@+id/main_label"     android:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_marginBottom="10px"android:text="Choose an option to continue:" /><Button     android:id="@+id/main_internal_storage_button"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="Use internal storage" /><Button android:id="@+id/main_external_storage_button"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="Use external storage" /></LinearLayout>
create layout file internal_storage.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:id="@+id/internal_storage_label"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_marginBottom="10px"android:text="Enter some text to write on the internal storage, then read back:" /><EditText android:id="@+id/internal_storage_input"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_marginBottom="10px" /><Button android:id="@+id/internal_storage_write_button"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="Write" /><Button android:id="@+id/internal_storage_read_button"android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="Read" /><TextView android:id="@+id/internal_storage_output"android:layout_width="fill_parent" android:layout_height="wrap_content"android:layout_marginBottom="10px" /></LinearLayout>

create main activity:

public class Main extends Activity implements OnClickListener {private Button internalStorage;private Button externalStorage;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                this.internalStorage = (Button) findViewById(R.id.main_internal_storage_button);        this.internalStorage.setOnClickListener(this);        this.externalStorage = (Button) findViewById(R.id.main_external_storage_button);        this.externalStorage.setOnClickListener(this);    }@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(v.equals(internalStorage)){startActivity(new Intent(this,InternalStorage.class));}else if(v.equals(externalStorage)){startActivity(new Intent(this,ExternalStorage.class));}}}
create InternalStorage activity:

public class InternalStorage extends Activity {   // no utils here, just basic java.io   // also be aware of getCacheDir, which writes to an internal   //directory that the system may clean up   private static final String LINE_SEP=System.getProperty("line.separator");   private EditText input;   private TextView output;   private Button write;   private Button read;      @Override   public void onCreate(final Bundle icicle) {      super.onCreate(icicle);      this.setContentView(R.layout.internal_storage);      this.input = (EditText) findViewById(R.id.internal_storage_input);      this.output = (TextView) findViewById(R.id.internal_storage_output);      this.write = (Button) findViewById(R.id.internal_storage_write_button);      this.write.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubwrite();}            });      this.read = (Button) findViewById(R.id.internal_storage_read_button);      this.read.setOnClickListener(new OnClickListener() {         public void onClick(final View v) {            read();         }      });   }   private void write(){   FileOutputStream fos=null;   try{   //obtain a FileOutputStream with openFileOutput   fos=openFileOutput("test.txt",Context.MODE_PRIVATE);   fos.write(input.getText().toString().getBytes());   Toast.makeText(this, "File writen", Toast.LENGTH_SHORT).show();   input.setText("");   output.setText("");   }catch(FileNotFoundException e){   Log.e(Constants._COUNT, "File not found", e);   }catch(IOException e){   Log.e(Constants._COUNT, "IO problem", e);   }finally{   try{   fos.close();   }catch(IOException e){      }   }   }   private void read(){   FileInputStream fis=null;   Scanner scanner=null;   StringBuilder sb=new StringBuilder();   try{   fis=openFileInput("test.txt");   // scanner does mean one more object, but it's easier to work with   scanner=new Scanner(fis);   while(scanner.hasNextLine()){   sb.append(scanner.nextLine()+LINE_SEP);   }   Toast.makeText(this, "File read", Toast.LENGTH_SHORT).show();   }catch(FileNotFoundException e){   Log.e(Constants._COUNT, "File not found", e);   }finally{   if(fis!=null){   try{   fis.close();   }catch(IOException e){      }   }   if(scanner!=null){   scanner.close();   }   }   output.setText(sb);   }}

Reading and writing data to and from filesystem, internal or external, canblock the main UI thread. In a real implementation, you’ll want to do thisfrom a Handler or an AsyncTask (passing in the file reference).


更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 精通android2第一章学习
  2. 安卓虚拟机启动后报错: 类似 SDK Manager]
  3. Android数据库ORMlite框架翻译系列(第一章
  4. android UI进阶之仿iphone的tab效果 (Andr
  5. Android加固技术分析与多渠道打包实践
  6. Android(安卓)技术-- 图形系统详解
  7. Android开发人员必须掌握的10 个开发工具
  8. Android系统服务接口
  9. [Android]LayoutInflater的inflate方法半
  10. Android我还可以相信你多少系列文章五之