Android App将数据写入内部存储和外部存储的示例

Android App将数据写入内部存储和外部存储的示例,第1张

概述File存储内部存储)一旦程序在设备安装后,data/data/包名/即为内部存储空间,对外保密。

file存储(内部存储)
一旦程序在设备安装后,data/data/包名/ 即为内部存储空间,对外保密。
Context提供了2个方法来打开输入、输出流

fileinputStream openfileinput(String @R_301_6889@) fileOutputStream openfileOutput(String @R_301_6889@,int mode)@H_301_12@
public class MainActivity extends Activity {  private TextVIEw show;  private EditText et;  private String file@R_301_6889@ = "test";  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    show = (TextVIEw) findVIEwByID(R.ID.show);    et = (EditText) findVIEwByID(R.ID.et);    findVIEwByID(R.ID.write).setonClickListener(new VIEw.OnClickListener() {      @OverrIDe      public voID onClick(VIEw v) {        try {          fileOutputStream fos = openfileOutput(file@R_301_6889@,Context.MODE_PRIVATE);          //fileOutputStream是字节流,如果是写文本的话,需要进一步把fileOutputStream包装 UTF-8是编码          OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");          //写          osw.write(et.getText().toString());          osw.flush();          fos.flush();          osw.close();          fos.close();        } catch (fileNotFoundException e) {          e.printstacktrace();        } catch (UnsupportedEnCodingException e) {          // Todo auto-generated catch block          e.printstacktrace();        } catch (IOException e) {          // Todo auto-generated catch block          e.printstacktrace();        }      }    });      findVIEwByID(R.ID.read).setonClickListener(new VIEw.OnClickListener() {      @OverrIDe      public voID onClick(VIEw v) {        try {          fileinputStream fis = openfileinput(file@R_301_6889@);          //当输入输出都指定字符集编码的时候,就不会出现乱码的情况          inputStreamReader isr = new inputStreamReader(fis,"UTF-8");          //获取文件的可用长度,构建一个字符数组          char[] input = new char[fis.available()];          isr.read(input);          isr.close();          fis.close();          String readed = new String(input);          show.setText(readed);        } catch (fileNotFoundException e) {          e.printstacktrace();        } catch (UnsupportedEnCodingException e) {          // Todo auto-generated catch block          e.printstacktrace();        } catch (IOException e) {          // Todo auto-generated catch block          e.printstacktrace();        }      }    });        }  }

data/data/package@R_301_6889@/files/test就是我们写入的文件。

SD存储(外部存储)
mnt/sdcard 目录就是SD卡的挂载点(只是一个指向)。
storage/sdcard: 真正的SD卡 *** 作目录。

一、文件下载
AndroID开发中,有时需要从网上下载一些资源以供用户使用,AndroID API中已经提供了很多直接可以用的类供大家使用,一般文件下载需要通过三个步骤:
1.创建一个httpURLConnection对象

@H_301_12@
// 创建一个URL对象,该对象包含一个IP地址,urlStr指的是网络IP地址 url = new URL(urlStr); // 通过URL对象,来创建一个httpURLConnection对象httpURLConnection urlConn = (httpURLConnection) url.openConnection();

2.获得一个inputStream对象

@H_301_12@
inputStream input = urlConn.getinputStream();

3.设置访问网络的权限

@H_301_12@
//在AndroIDManifest.xml配置文件中加入权限信息 <uses-permission androID:@R_301_6889@="androID.permission.INTERNET"/>

二、访问并写入SD卡
1.判断手机上是否插入SD卡,且应用程序具有读写权限

@H_301_12@
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

2.得到当前SD卡的目录

@H_301_12@
Environment.getExternalStorageDirectory();

3.在访问SD卡前还必须在配置文件中设置权限,这样才可以最SD卡进行存取 *** 作

@H_301_12@
<uses-permission androID:@R_301_6889@="androID.permission.WRITE_EXTERNAL_STORAGE"/>

以下是一个对SD *** 作经常用到的封装类,以后如果需要对SD卡 *** 作,直接可以拿过来用

@H_301_12@
public class fileUtils {  private String Sdpath;  public String getSdpath(){    return Sdpath;  }  //构造函数,得到SD卡的目录,这行函数得到的目录名其实是叫"/SDCARD"  public fileUtils() {    Sdpath = Environment.getExternalStorageDirectory() +"/";  }  //在SD卡上创建文件  public file createSDfile(String file@R_301_6889@) throws IOException{    file file = new file(Sdpath + file@R_301_6889@);    file.createNewfile();    return file;  }  //在SD卡上创建目录  public file createSDDir(String dir@R_301_6889@){    file dir = new file(Sdpath + dir@R_301_6889@);    dir.mkdir();    return dir;  }  //判断SD卡上的文件夹是否存在  public boolean isfileExist(String file@R_301_6889@){    file file = new file(Sdpath + file@R_301_6889@);    return file.exists();  }  //将一个inputStream里面的数据写入到SD卡中  //将input写到path这个目录中的file@R_301_6889@文件上  public file write2SDFrominput(String path,String file@R_301_6889@,inputStream input){    file file = null;     OutputStream output = null;     try{       createSDDir(path);       file = createSDfile(path + file@R_301_6889@);       //fileinputStream是读取数据,fileOutputStream是写入数据,写入到file这个文件上去      output = new fileOutputStream(file);       byte buffer [] = new byte[4 * 1024];       while((input.read(buffer)) != -1){         output.write(buffer);       }       output.flush();     }     catch(Exception e){       e.printstacktrace();     }     finally{       try{         output.close();       }       catch(Exception e){         e.printstacktrace();       }     }     return file;   } }

总结

以上是内存溢出为你收集整理的Android App将数据写入内部存储和外部存储的示例全部内容,希望文章能够帮你解决Android App将数据写入内部存储和外部存储的示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1149506.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存