カテゴリー
SugiBlog Webデザイナー・プログラマーのためのお役立ちTips

assetsフォルダーにあるテキストファイルを読み込む

assetsフォルダーに格納したテキストファイルを読み込みます。

InputStream inputStream = null;
BufferedReader reader = null;
try {
    inputStream = getResources().getAssets().open("ファイル名");
    reader = new BufferedReader(
              new InputStreamReader(inputStream));
    String line;
    StringBuilder sb = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        Log.v("reader", line);
        if(line != ""){
            sb.append(line);
        }
    }
    String str = sb.toString();
    Toast.makeText(this, str, Toast.LENGTH_LONG).show();

} catch (IOException e) {
    e.printStackTrace();
} catch (SQLException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        reader.close();
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
7,080 views

New I/Oの高速ファイル入出力

FileクラスにはCopyなどというメソッドがないようなので、調べていたらNew I/Oというものを発見したのでメモ。

java.nio.channels.FileChannelを使用して、簡単にコピーできます。

File inputFile  = new File("入力元ファイルのパス");
File outputFile = new File("出力先ファイルのパス");

try {
    FileChannel inputChannel  = new FileInputStream(inputFile).getChannel();
    FileChannel outputChannel = new FileOutputStream(outputFile).getChannel();

    // インプットチャネルの出力をアウトプットチャネルに接続
    inputChannel.transferTo(0, inputChannel.size(), outputChannel);

    inputChannel.close();
    outputChannel.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
2,710 views

音声認識 RecognizerIntent

簡単な音声認識を使用する

private static final int REQCODE = 1234;

private void voiceRecognize() {
    try {
        // インテントを作成
        Intent intent = new Intent(
                RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        // ウェブサーチを使う場合は
        // ACTION_WEB_SEARCH

        intent.putExtra(
                RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(
                RecognizerIntent.EXTRA_PROMPT,
                "音声認識テスト");

        // アクティビティの呼び出し
        startActivityForResult(intent, REQUEST_CODE);

    } catch (ActivityNotFoundException e) {
        // 音声認識に対応していない場合
        Toast.makeText(this,
                "音声認識に対応していません",
                Toast.LENGTH_LONG).show();
    }
}

続きを読む…»

2,781 views

メール送信(メーラーをGmailに指定)

メーラーを起動する際に、起動するメーラーをGmailに指定したい場合

Intent intent = new Intent(Intent.ACTION_SEND);

String[] strTo = { "to@example.jp" };

intent.putExtra(Intent.EXTRA_EMAIL, strTo);
intent.putExtra(Intent.EXTRA_SUBJECT, "件名");
intent.putExtra(Intent.EXTRA_TEXT, "本文\n");

intent.setType("message/rfc822");

intent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");

startActivity(intent);

複数の宛先への送信や、添付ファイルについては[他アプリと連携]を参照

6,556 views

ファイル入出力(SDカード)

ローカルファイルへのアクセスについては[ローカルファイルのアクセス]、[ファイル入出力 [Android]]でも紹介しています。

同様の方法でおこなうとSDカード上のファイルの読み書きはできません。
ローカルファイルを読み込み・保存する場合はopenFileOutputメソッド等を使用しますが、引数に指定するファイルパスはフルパスではなく、ファイル名のみです。
引数に「/」(パスのセパレイター)が使用できなくなっています。

[スクリーンショットの保存]でも保存方法の違いを書いていますが、改めてSDカードのファイル入出力について書いておきます。

テキストファイルの読み書きを例とします。

テキストファイルの読み込み

private void readFile(String filename) {

    FileInputStream inputStream = null;
    BufferedReader reader = null;

    try {
        inputStream = new FileInputStream(filename);
        reader = new BufferedReader(
                new InputStreamReader(inputStream));
        String line;
        while ((line = reader.readLine()) != null) {
            //Log.v(TAG, line);
            //読み込んだデータの処理を記述
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            reader.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

テキストファイルの書き込み

private void writeFile(String filename, String src){
    try {
        FileOutputStream outputStream = new FileOutputStream(filename);
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(outputStream));
        writer.write(src);
        //writer.flush(); //flushでも書き込んでくれるよう
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
2,584 views