assetsフォルダも同様です。
このフォルダのファイルは読み込みは出来ますが、書き込みはできません。
どのように使い分ければ良いのか調べたのですが・・・よくわかりませんでした。
たぶんですが
アプリで使用するようなアイコンなどはres/rawフォルダ
データベースのテーブルを作成するsqlファイルのようなものはassetsフォルダ
res/rawフォルダのファイルを読み込むには
まずEclipseのパッケージエクスプローラより、res/rawフォルダにテキストファイルを作成します。
(rawフォルダがない場合は作成します。)
ファイル名に許可されている文字はa~zの小文字、アンダーバー(_)、ピリオド(.)のみです。
rawフォルダに配置したファイルは以下のようにInputStreamインスタンスを取得できます。
Resources res = this.getResources(); InputStream is = res.openRawResource(R.raw.ファイル名);
assetsフォルダのファイルを読み込むには
まずEclipseのパッケージエクスプローラより、assetsフォルダにテキストファイルを作成します。
ファイル名にはres/rawフォルダのような制限はありません。
assetsフォルダに配置したファイルは以下のようにInputStreamインスタンスを取得できます。
AssetManager as = getResources().getAssets();
InputStream is = as.open("ファイル名.txt");
サンプルコード
MainActivity.java
package my.study.android;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.app.Activity;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
/*
* onCreate
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//ResRawボタン
Button btnResRaw = (Button)this.findViewById(R.id.btnResRaw);
btnResRaw.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {buttonResRaw_OnClick(v);}});
//Readボタン
Button btnAssets = (Button)this.findViewById(R.id.btnAssets);
btnAssets.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {buttonAssets_OnClick(v);}});
}
/*
* ResRawボタン処理
*/
private void buttonResRaw_OnClick(View v){
Resources res = this.getResources();
InputStream is = null;
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
try{
try {
is = res.openRawResource(R.raw.textraw);
br = new BufferedReader(new InputStreamReader(is));
String str;
while((str = br.readLine()) != null){
sb.append(str +"\n");
}
} finally {
if (br !=null) br.close();
}
} catch (IOException e) {
Toast.makeText(this, "読み込み失敗", Toast.LENGTH_SHORT).show();
}
TextView label = (TextView)this.findViewById(R.id.label);
label.setText(sb.toString());
}
/*
* AssetsボタンClick処理
*/
private void buttonAssets_OnClick(View v){
AssetManager as = getResources().getAssets();
InputStream is = null;
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
try{
try {
is = as.open("TextAssets.txt");
br = new BufferedReader(new InputStreamReader(is));
String str;
while((str = br.readLine()) != null){
sb.append(str +"\n");
}
} finally {
if (br != null) br.close();
}
} catch (IOException e) {
Toast.makeText(this, "読み込み失敗", Toast.LENGTH_SHORT).show();
}
TextView label = (TextView)this.findViewById(R.id.label);
label.setText(sb.toString());
}
}
main.xml(MainActivityのレイアウト)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/label"></TextView>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Res/Raw" android:id="@+id/btnResRaw"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Assets" android:id="@+id/btnAssets"></Button>
</LinearLayout>
0 件のコメント:
コメントを投稿