android in practice_Using internal storage(FileExplorer project)
16lz
2021-01-26
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).
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer