1,文件保存数据
输出流:OpenFileOutput
输入流:OpenFileInput
2,将数据报讯到内存卡里面
操作内存卡需要一下三个权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />挂载权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />写权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />读取权限
//获得外部存储卡
//File file = Environment.getExternalStorageDirectory();
//获取制定文件sdcard位置的目录
File file =Environment.getExternalStoragePublicDirectory("DCIM");
3,android工程中获取资源的引用;
使用文件的方式保存数据
1,创建布局文件 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/text_v1" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center_horizontal" android:hint="输入框" /> <Button android:id="@+id/Save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text_v1" android:onClick="show" android:text="保 存" /> <Button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/Save" android:layout_below="@id/text_v1" android:onClick="show" android:text="清除输入框" /> <Button android:id="@+id/Read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/Save" android:onClick="show" android:text=" 读 取 " /> </RelativeLayout>
FileDemo操作数据,并显示
public class FileDemo extends Activity { private EditText text_v1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text_v1=(EditText)this.findViewById(R.id.text_v1); } // 按钮的监听器方法 public void show(View view) { switch (view.getId()) { case R.id.Save:// 保存 try { //将数据写到文件中 //参数一:文件名 //参数二:文件创建的模式;私有模式 FileOutputStream fos=this.openFileOutput("FileDemo", MODE_PRIVATE); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject("保存成内部文件的方式操作"); oos.flush(); oos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); break; case R.id.Read: //读取保存的文件的数据 try { //读取文件的数据 FileInputStream fis=this.openFileInput("FileDemo"); ObjectInputStream ois =new ObjectInputStream(fis); String str =(String)ois.readObject(); text_v1.setText(str); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case R.id.clear: //清空 text_v1.setText(""); break; } } }
数据保存到内存卡 关键代码
1,创建布局文件acivity_main.xml ,
<Button android:id="@+id/Save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text_v1" android:onClick="show" android:text="保 存" />
操作布局的文件
// 按钮的监听器方法 public void show(View view) { switch (view.getId()) { case R.id.Save:// 保存 //获得外部存储卡 //File file = Environment.getExternalStorageDirectory(); //获得制定sdcard位置的目录 File file =Environment.getExternalStoragePublicDirectory("DCIM"); //在DCIM的目录下创建文件C++++++++++++++ File files= new File(file,"C++++++++++++++"); if(!files.exists()){ Boolean b=file.mkdir(); if(!b){ Toast.makeText(this, "不能创建", Toast.LENGTH_SHORT).show(); } } if(!files.canWrite()){ Toast.makeText(this, "不能写文件", Toast.LENGTH_SHORT).show(); } //在C++++++++++++++的目录下创建文件abc File file1 = new File(files,"abc"); try { FileOutputStream fos = new FileOutputStream(file1); fos.write("长沙".getBytes()); fos.flush(); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); break; }
3,android工程中使用静态资源的方式
读取assets和res文件下的raw资源
运行效果:
详细代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/text_v1" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center_horizontal" android:hint="输入框" /> <Button android:id="@+id/Save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text_v1" android:onClick="show" android:text="保 存" /> <Button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/Save" android:layout_below="@id/text_v1" android:onClick="show" android:text="清除输入框" /> <Button android:id="@+id/Read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/Save" android:onClick="show" android:text=" 读 取 " /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/Read" android:layout_below="@+id/Read" android:layout_marginLeft="56dp" android:layout_marginTop="154dp" android:ems="10" android:inputType="textMultiLine"/> </RelativeLayout>
Dmo操作数据的显示,读取assets和res文件下的raw资源
public class Demo extends Activity{ private ImageView img; private EditText text_v1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img=(ImageView)this.findViewById(R.id.imageView1); text_v1=(EditText)this.findViewById(R.id.text_v1); } public void show(View v){ switch (v.getId()) { case R.id.Save: //读取assets资源文件中的文件,使用流读取 try { //使用流读取Assets中的资源 InputStream is =this.getResources().getAssets().open("text.txt"); byte[] by= new byte[is.available()]; is.read(by); String str = EncodingUtils.getString(by,"GBK"); text_v1.setText(str); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case R.id.Read: //读取assets资源文件中的图片 try { InputStream ism =this.getResources().getAssets().open("img_1.jpg"); byte[] bs = new byte[ism.available()]; ism.read(bs); //将荼毒到的字节转化成bmp格式的图片 Bitmap bmp=BitmapFactory.decodeByteArray(bs, 0, bs.length); //设置图片显示 img.setImageBitmap(bmp); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } break; case R.id.clear: //res文件中创建的raw文件下的文件 InputStream is1=this.getResources().openRawResource(R.raw.text1); byte[] by; try { by = new byte[is1.available()]; is1.read(by); String str1 = EncodingUtils.getString(by,"GBK"); text_v1.setText(str1); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } } }