服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android App将数据写入内部存储和外部存储的示例

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

2021-06-30 16:09chenlijian Android

这篇文章主要介绍了Android App将数据写入内部存储和外部存储的示例,使用外部存储即访问并写入SD卡,需要的朋友可以参考下

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

  • fileinputstream openfileinput(string name)
  • fileoutputstream openfileoutput(string name, int mode)
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class mainactivity extends activity {
 
  private textview show;
  private edittext et;
  private string filename = "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(filename, 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(filename);
          //当输入输出都指定字符集编码的时候,就不会出现乱码的情况
          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/packagename/files/test就是我们写入的文件。

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

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

?
1
2
3
4
// 创建一个url对象,该对象包含一个ip地址,urlstr指的是网络ip地址
url = new url(urlstr);
// 通过url对象,来创建一个httpurlconnection对象
httpurlconnection urlconn = (httpurlconnection) url.openconnection();

2.获得一个inputstream对象

?
1
inputstream input = urlconn.getinputstream();

3.设置访问网络的权限

?
1
2
//在androidmanifest.xml配置文件中加入权限信息
<uses-permission android:name="android.permission.internet"/>

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

?
1
environment.getexternalstoragestate().equals(environment.media_mounted);

2.得到当前sd卡的目录

?
1
environment.getexternalstoragedirectory();

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

?
1
<uses-permission android:name="android.permission.write_external_storage"/>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class fileutils {
  private string sdpath;
  public string getsdpath(){
    return sdpath;
  }
  //构造函数,得到sd卡的目录,这行函数得到的目录名其实是叫"/sdcard"
  public fileutils() {
    sdpath = environment.getexternalstoragedirectory() +"/";
  }
  //在sd卡上创建文件
  public file createsdfile(string filename) throws ioexception{
    file file = new file(sdpath + filename);
    file.createnewfile();
    return file;
  }
  //在sd卡上创建目录
  public file createsddir(string dirname){
    file dir = new file(sdpath + dirname);
    dir.mkdir();
    return dir;
  }
  //判断sd卡上的文件夹是否存在
  public boolean isfileexist(string filename){
    file file = new file(sdpath + filename);
    return file.exists();
  }
 
  //将一个inputstream里面的数据写入到sd卡中
  //将input写到path这个目录中的filename文件上
  public file write2sdfrominput(string path, string filename, inputstream input){
    file file = null;
    outputstream output = null;
    try{
      createsddir(path);
      file = createsdfile(path + filename);
      //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;
  }
}

延伸 · 阅读

精彩推荐