求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   模型库  
会员   
 


AI 智能化软件测试方法与实践
5月23-24日 上海+在线



人工智能.机器学习TensorFlow
5月22-23日 北京



图数据库与知识图谱
5月22-23日 北京
 
 
 

Android开发教程
Android 开发环境配置
Android 架构
Android 应用组件
Android Hello World示例
Android 资源组织和访问
Android Activity
Android Service
Android 广播接收器
Android 内容提供者
Android 碎片/片段
Android Intent过滤器
Android UI布局
Android UI控件
Android 事件处理
Android 样式和主题
Android 自定义组件
Android 拖放
Android 通知
Android 基于位置服务
Android 发送电子邮件
Android 发送短信/SMS
Android 拨打电话
发布Android应用
ndroid Alertdialog
Android Animation实例
Android音频捕获
Android音频管理器实例
Android
Android最佳实践
Android Bluetooth实例
Android Camera
Android Clipboard
Android自定义字体
Android数据备份
Android Gestures/手势
Android图片效果
Android图片切换
Android内部存储
Android JetPlayer实例
Android JSON解析器
Android加载Spinner
Android本地化
Android登录实例
Android MediaPlayer
 
 

Android内部存储
1161 次浏览
42次  

Android提供多种存储的应用程序存储自己的数据。存储的地点是共享的,内部和外部存储,SQLite存储,并通过网络连接存储。

在本章中,我们要看看在内部存储。内部存储是设备存储器上的专用数据的存储。

默认情况下,这些文件是私有并由唯一应用程序访问和删除,当用户删除应用程序。

写入文件

为了使用内部存储写入某些数据到文件中,调用openFileOutput()方法用的文件和模式的名称。该模式可以是?private , public,它的语法如下:

FileOutputStream fOut = openFileOutput("file name here",MODE_WORLD_READABLE);

该方法openFileOutput()返回FileOutputStream的一个实例。因此收到FileInputStream对象。之后可以调用write方法写入文件数据。它的语法如下:

String str = "data";  fOut.write(str.getBytes());  fOut.close();

读取文件

为了从刚才创建的文件中读取数据,openFileOutput()方法使用文件的名称。它返回FileInputStream的一个实例。它的语法如下:

FileInputStream fin = openFileInput(file);

在此之后,可以调用read方法来一次从文件读取一个字符,然后打印出来。它的语法如下:

int c;
    String temp="";
    while( (c = fin.read()) != -1){
       temp = temp + Character.toString((char)c);
    }
     //string temp contains all the data of the file.
     fin.close();

除了写入(write)和关闭(close)方法,对于更好写入文件所提供FileOutputStream类的其他方法。这些方法如下:?

除了用于良好的读取文件所提供的FileInputStreamclass读取和关闭,还有其它方法的方法。这些方法如下:

例子

这里有一个例子演示如何使用内部存储来存储和读取文件。它创建了一个基本的存储应用程序,它可以从内部存储读取并写入。

为了试验这个例子,可以在实际设备或模拟器运行此。

以下是修改的主活动文件的内容?src/com.yiibai.storage/MainActivity.java.

package com.example.storage;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    public class MainActivity extends Activity {
       private EditText et;
       private String data;
       private String file = "mydata";
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText)(findViewById(R.id.editText1));
       }
       public void save(View view){
        data = et.getText().toString();
        try {
           FileOutputStream fOut = openFileOutput(file,MODE_WORLD_READABLE);
           fOut.write(data.getBytes());
           fOut.close();
           Toast.makeText(getBaseContext(),"file saved",
           Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }
     }
     public void read(View view){
        try{
           FileInputStream fin = openFileInput(file);
           int c;
           String temp="";
           while( (c = fin.read()) != -1){
              temp = temp + Character.toString((char)c);
           }
           et.setText(temp);
           Toast.makeText(getBaseContext(),"file read",
           Toast.LENGTH_SHORT).show();
          }catch(Exception e){
          }
     }
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
     }
    }

以下是XML修改res/layout/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" >
       <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="182dp"
        android:onClick="save"
        android:text="@string/save" />
       <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="46dp"
        android:onClick="read"
        android:text="@string/read" />
       <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="23dp"
        android:ems="10"
        android:inputType="textMultiLine" >
          <requestFocus />
     </EditText>
   </RelativeLayout>

以下是?res/values/string.xml. 的内容

<?xml version="1.0" encoding="utf-8"?>
    <resources>
       <string name="app_name">Storage</string>
       <string name="action_settings">Settings</string>
       <string name="hello_world">Hello world!</string>
       <string name="save">save to intenal storage</string>
       <string name="read">load from intenal storag</string>
    </resources>

以下是?AndroidManifest.xml?的内容

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.yiibai.storage"
     android:versionCode="1"
     android:versionName="1.0" >
       <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
       <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
           android:name="com.yiibai.storage.MainActivity"
           android:label="@string/app_name" >
           <intent-filter>
              <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
        </activity>
     </application>
  </manifest>

让我们试着来运行刚刚修改的存储应用程序。安装程序在AVD并启动它,如果一切设置和应用程序都没有问题,它会显示以下仿真器窗口:

现在,需要做的是在字段中输入文本。举例来说:这里已经进入SOEM文本。按保存“save”按钮。以下通报会出现在AVD如下:

现在,当按下Load按钮,应用程序将读取该文件,并显示数据。如下面的数据将返回:

注意:可以通过切换到DDMS标签查看此文件。在DDMS选择文件浏览器和浏览这个路径。

data>data>com.example.storage>files>mydata

这也显示在下面图中:


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



1161 次浏览
42次
 捐助