ともちゃんのアプリ開発日記

組込みC言語プログラマだったともちゃんが、四苦八苦しながら、AndroidのJAVA/Kotlin、iOSのSwiftUIを習得して行きます。ともちゃんの備忘録も兼ねています。

Uriで指定されたファイルの読み込み

暗黙的インテントでアプリが起動される場合、Uriでファイルが指定されることがあります。その場合、ContentResolverを駆使しなくても、ファイルを読みだすことが可能です。

UriCSVファイルが指定されている場合を例にとって、説明していきます。

1.Uriで指定されたファイルのオープン

InputStream inputStream = context.getContentResolver().openInputStream(importUri);

 2.InputStream型→InpurStreamReader型→BufferReader型変換

バイナリファイルの場合、

byte[] buffer = new Byte[1024];
inputStream.read(buffer);

で読み込めますが、テキストファイルの場合、1行単位で読み出したいので、InputStream型を、InpurStreamReader型→BufferReader型に変換する必要があります。

そのやり方がこうです。

InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);

そして、

String AcpplicationTitle = reader.readLine();

 

で読み込みます

3.ソースコード全体

    public int importFile(Context context, Uri importUri, ProgressDialog progressDialog) {
// File srcFile = new File(CSVfile);
// InputStream inputFile;
try {
InputStream inputStream = context.getContentResolver().openInputStream(importUri);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
String AcpplicationTitle = reader.readLine();
if (!AcpplicationTitle.equals(context.getText(R.string.app_name).toString())) {
return NOT_DIET_REC_FILE;
}
String strVersionCode = reader.readLine();

String[] strs;
strs = strVersionCode.split(",");
int version = Integer.valueOf(strs[1]);
switch (version) {
case 1:
String PrefecensesTitle = reader.readLine();
String PrefecensesData = reader.readLine();
strs = PrefecensesData.split(",");
if (strs.length != 4) {
return INVALID_PREFERENCE_DATA_COUNT;
}
float targetTall = Float.valueOf(strs[0]);
float targetWeight = Float.valueOf(strs[1]);
float targetBodyFatPercentage = Float.valueOf(strs[2]);
float targetBMI = Float.valueOf(strs[3]);
SharedPreferences prefs = context.getSharedPreferences("setting", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putFloat("TargetTall", targetTall);
editor.putFloat("TargetWeight", targetWeight);
editor.putFloat("TargetBodyFatPercentage", targetBodyFatPercentage);
editor.putFloat("TargetBMI", targetBMI);
// editor.commit();
editor.apply();

String strDataCount = reader.readLine();
strs = strDataCount.split(",");
long dataCount = Long.valueOf(strs[1]);
String DataTitle = reader.readLine();
DatabaseManager databaseManager = new DatabaseManager();
progressDialog.setMax((int) dataCount);
for (long i = 0; i < dataCount; i++) {
String strData = reader.readLine();
strs = strData.split(",");
switch (strs.length) {
case 11:
databaseManager.replaceDatabase(
strs[0], // 日付
Float.valueOf(strs[1]), // 体重
Float.valueOf(strs[2]), // 体脂肪率
Float.valueOf(strs[3]), // BMI
Float.valueOf(strs[4]), // 目標体重
Float.valueOf(strs[5]), // 目標体脂肪率
Float.valueOf(strs[6]), // 目標BMI
Integer.valueOf(strs[7]), // スタンプ●
Integer.valueOf(strs[8]), // スタンプ×
Integer.valueOf(strs[9]), // スタンプ▲
Integer.valueOf(strs[10]), // スタンプ★
""); // メモ
break;
case 12:
databaseManager.replaceDatabase(
strs[0], // 日付
Float.valueOf(strs[1]), // 体重
Float.valueOf(strs[2]), // 体脂肪率
Float.valueOf(strs[3]), // BMI
Float.valueOf(strs[4]), // 目標体重
Float.valueOf(strs[5]), // 目標体脂肪率
Float.valueOf(strs[6]), // 目標BMI
Integer.valueOf(strs[7]), // スタンプ●
Integer.valueOf(strs[8]), // スタンプ×
Integer.valueOf(strs[9]), // スタンプ▲
Integer.valueOf(strs[10]), // スタンプ★
strs[11]); // メモ
break;
default:
return INVALID_DB_DATA_COUNT;
}
progressDialog.setProgress((int) i);
try {
Thread.sleep(WAIT_TIME); //200ミリ秒Sleepする
} catch (InterruptedException e) {
}
}
break;
}
} catch(FileNotFoundException e) {
return FAIL_IN_FILE_OPEN;
} catch(IOException e) {
return FAIL_IN_FILE_READ;
}

return SUCCESS;
}
}