facebook twitter hatena line email

「Android/ローカルファイル」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(Admin がページ「Android/ローカルファイル読み書き」を「Android/ローカルファイル」に、リダイレクトを残さずに移動しました)
(相違点なし)

2018年11月29日 (木) 11:35時点における版

読み込みサンプル

String html = FileLocalUtil.read(getApplicationContext(), "index.html");
  • src/info/nonip/android/lib/util/FileLocalUtil.java
package info.nonip.android.lib.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.util.Log;
/**
 * ファイル読み書きユーティリティ
 */
public class FileLocalUtil {
  // ファイル読み込み
  public static String read(Context context, String filepath) {
    try {
      InputStream fileInputStream = context.getAssets().open(filepath);
      byte[] readBytes = new byte[fileInputStream.available()];
      fileInputStream.read(readBytes);
      String str = new String(readBytes);
Log.d("FileUtil", "file=" + filepath + " str=" + str);
    return str;
    } catch (FileNotFoundException e) {
      Log.e("error", e.toString());
      return "";
    } catch (IOException e) {
      Log.e("error", e.toString());
      return "";
    }
  }
}
  • assets/index.html
hoge

assetsはmainの下(javaやresと兄弟の部分に置く)