Android-internal-storage
Android-内部ストレージ
Androidは、アプリケーションがデータを保存するためのさまざまな種類のストレージを提供します。 これらの保存場所は、共有設定、内部および外部ストレージ、SQLiteストレージ、およびネットワーク接続を介したストレージです。
この章では、内部ストレージについて説明します。 内部ストレージは、デバイスメモリ上のプライベートデータのストレージです。
デフォルトでは、これらのファイルはプライベートであり、アプリケーションのみがアクセスし、ユーザーがアプリケーションを削除すると削除されます。
書き込みファイル
内部ストレージを使用してファイルにデータを書き込むには、ファイルの名前とモードを指定してopenFileOutput()メソッドを呼び出します。 モードはprivate、public e.t.cなどです。 その構文は以下のとおりです-
FileOutputStream fOut = openFileOutput("file name here",MODE_WORLD_READABLE);
メソッドopenFileOutput()は、FileOutputStreamのインスタンスを返します。 したがって、FileInputStreamのオブジェクトで受信します。 その後、writeメソッドを呼び出してファイルにデータを書き込むことができます。 その構文は以下のとおりです-
String str = "data";
fOut.write(str.getBytes());
fOut.close();
読み込みファイル
作成したファイルから読み取るには、ファイルの名前でopenFileInput()メソッドを呼び出します。 FileInputStreamのインスタンスを返します。 その構文は以下のとおりです-
FileInputStream fin = openFileInput(file);
その後、readメソッドを呼び出して、ファイルから一度に1文字ずつ読み取り、印刷することができます。 その構文は以下のとおりです-
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 クラスによって提供される、ファイルをより適切に書き込むためのメソッドがあります。 これらの方法は以下のとおりです-
Sr.No | Method & description |
---|---|
1 |
FileOutputStream(File file, boolean append) このメソッドは、ファイルに書き込む新しいFileOutputStreamを構築します。 |
2 |
getChannel() このメソッドは、このストリームと位置を共有する書き込み専用FileChannelを返します |
3 |
getFD() このメソッドは、基礎となるファイル記述子を返します |
4 |
write(byte[] buffer, int byteOffset, int byteCount) このメソッドは、このストリームへのオフセット位置から始まるバイト配列バッファからcountバイトを書き込みます |
readおよびcloseのメソッドとは別に、 FileInputStream クラスによって提供される、ファイルをより適切に読み取るためのその他のメソッドがあります。 これらの方法は以下のとおりです-
Sr.No | Method & description |
---|---|
1 |
available() このメソッドは、追加の入力をブロックせずに読み取りまたはスキップできる推定バイト数を返します |
2 |
getChannel() このメソッドは、このストリームと位置を共有する読み取り専用FileChannelを返します |
3 |
getFD() このメソッドは、基礎となるファイル記述子を返します |
4 |
read(byte[] buffer, int byteOffset, int byteCount) このメソッドは、このストリームから最大で長さのバイトを読み取り、オフセットで始まるバイト配列bに格納します |
例
以下は、ファイルを保存および読み取るための内部ストレージの使用を示す例です。 内部ストレージから読み書きできる基本的なストレージアプリケーションを作成します。
この例を試すには、実際のデバイスまたはエミュレーターでこれを実行できます。
Steps | Description |
---|---|
1 | You will use Android Studio IDE to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to add necessary code. |
3 | Modify the res/layout/activity_main to add respective XML components |
4 | Run the application and choose a running android device and install the application on it and verify the results |
以下は、変更されたメインアクティビティファイル src/MainActivity.java の内容です。
package com.example.sairamkrishna.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends Activity {
Button b1,b2;
TextView tv;
EditText ed1;
String data;
private String file = "mydata";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
ed1=(EditText)findViewById(R.id.editText);
tv=(TextView)findViewById(R.id.textView2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data=ed1.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();
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
FileInputStream fin = openFileInput(file);
int c;
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
tv.setText(temp);
Toast.makeText(getBaseContext(),"file read",Toast.LENGTH_SHORT).show();
}
catch(Exception e){
}
}
});
}
}
以下は、xml res/layout/activity_main.xml の変更されたコンテンツです。
'_次のコードで abc はfinddevguides.comのロゴを示します_
<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="Internal storage" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="@+id/textView"
android:layout_below="@+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="Enter Text"
android:focusable="true"
android:textColorHighlight="#ff7eff15"
android:textColorHint="#ffff25e6"
android:layout_below="@+id/imageView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:layout_marginTop="42dp"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="load"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read"
android:id="@+id/textView2"
android:layout_below="@+id/editText"
android:layout_toLeftOf="@+id/button2"
android:layout_toStartOf="@+id/button2"
android:textColor="#ff5bff1f"
android:textSize="25dp"/>
</RelativeLayout>
以下は res/values/string.xml の内容です。
<resources>
<string name="app_name">My Application</string>
</resources>
以下は AndroidManifest.xml ファイルの内容です。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
変更したStorageアプリケーションを実行してみましょう。 環境設定中に AVD を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像の実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-
Android Internal Stroage Tutorial
次に、フィールドにテキストを入力する必要があります。 たとえば、テキストを入力しました。 保存ボタンを押します。 次の通知がAVDに表示されます-
Android Internal Stroage Tutorial
ロードボタンを押すと、アプリケーションはファイルを読み取り、データを表示します。 私たちの場合、次のデータが返されます-
Android Internal Stroage Tutorial
DDMSタブに切り替えると、このファイルを実際に表示できることに注意してください。 DDMSで、ファイルエクスプローラーを選択し、このパスをナビゲートします。
tools>android>android device Monitor
これは、以下の画像にも示されています。