Android のファイルに関するネタを3つほどその第一弾です
1. アプリケーションに同梱(その1, 今回) English
2. どっかに保存したい(その2)
3. C言語用にパスが欲しい, とにかくパスが欲しい(その3)
○基本
Android でファイルを扱いたい, とくに, アプリケーションにBundle しておきたい場合, Application Project のどこかにいれておかなくてはなりません。
置き場所としては2つあります
1. res/raw
2. assets
違いについてはいろいろな方が説明されています。
基本的には, assets を使えばよいかと。 音楽ファイルや画像ファイルなど, リソース R.java で扱いたい場合は, res/raw を使えばよいのかと存じます。
※ res/raw はデフォルトでは存在しません。 自分で作らないといけません
○できること
ではこのディレクトリに入れたファイルをどう扱うかです。これらは, Activity クラスのメソッド(Context) を使って, InputStream を取得します。これだけです。ということはカキコできません。
○サンプルいきます
public class ApplicationFileActivity extends Activity
{
private static String TAG = "Application File";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
readFromAssets();
readFromRaw();
new FileOperations(this).readFromAssets();
}
private void readFromAssets()
{
AssetManager as = getResources().getAssets();
BufferedReader br = null;
try
{
InputStream in = as.open("test2.txt");
br = new BufferedReader(new InputStreamReader(in));
String line;
Log.i(TAG, "Assets");
while ( (line = br.readLine()) != null )
{
Log.i(TAG, line);
}
}
catch ( IOException oops )
{
oops.printStackTrace();
}
finally
{
if ( br != null )
{
try
{
br.close();
}
catch(IOException oops )
{
oops.printStackTrace();
}
}
}
}
private void readFromRaw()
{
InputStream in = getResources().openRawResource(R.raw.test); // test.txt
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
try
{
Log.i(TAG, "Raw");
while ( (line = br.readLine()) != null )
{
Log.i(TAG, line);
}
}
catch ( IOException oops )
{
oops.printStackTrace();
}
finally
{
if ( br != null )
{
try
{
br.close();
}
catch(IOException oops )
{
oops.printStackTrace();
}
}
}
}
}
test1.txt は, res/rawに, test2.txt は, assets に入れています。
内容は, 1行のテキストファイル(UTF-8)
結果は,
DDMS でLog を確認, ファイルの内容がなんとなく想像できます。
さて, Context を使ってちょっと書き換えてみます。
public class FileOperations
{
private static String TAG = "File Operations";
Context context;
public FileOperations ( Context context )
{
this.context = context;
}
public void readFromAssets()
{
AssetManager as = context.getResources().getAssets();
BufferedReader br = null;
try
{
InputStream in = as.open("test2.txt");
br = new BufferedReader(new InputStreamReader(in));
String line;
Log.i(TAG, "Assets");
while ( (line = br.readLine()) != null )
{
Log.i(TAG, line);
}
}
catch ( IOException oops )
{
oops.printStackTrace();
}
finally
{
if ( br != null )
{
try
{
br.close();
}
catch(IOException oops )
{
oops.printStackTrace();
}
}
}
}
public void readFromRaw()
{
InputStream in = context.getResources().openRawResource(R.raw.test); // test.txt
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
try
{
Log.i(TAG, "Raw");
while ( (line = br.readLine()) != null )
{
Log.i(TAG, line);
}
}
catch ( IOException oops )
{
oops.printStackTrace();
}
finally
{
if ( br != null )
{
try
{
br.close();
}
catch(IOException oops )
{
oops.printStackTrace();
}
}
}
}
}
これをActivity から呼ぶには
new FileOperations(this).readFromAssets();
○Programming 上の注意
メソッドを呼び出すには, android.contet.Context のインスタンスが必要ですですので, Activity 上
で行うか, 別のクラスで利用するには, Activity を渡してやる必要があります
○ これらのファイルのパスは?
さて, こいつらで問題になるのは, File クラスとして扱えないことです。
あくまで, InputStream などの Stream クラス系で提供されているので, File クラスを使って, delete したり, パスを取得することはできないのです。
それが 3 につながります。 パスを取得して, 何かをするときには, テクニックが必要です。
※ちなみにiOS だと, Resource ディレクトリとかを作ってそこに入れて, XCode 上でResource のところに入れておけばこんなに意識することはない!

0 件のコメント:
コメントを投稿