ローカルファイルへのアクセスについては[ローカルファイルのアクセス]、[ファイル入出力 [Android]]でも紹介しています。
同様の方法でおこなうとSDカード上のファイルの読み書きはできません。
ローカルファイルを読み込み・保存する場合はopenFileOutputメソッド等を使用しますが、引数に指定するファイルパスはフルパスではなく、ファイル名のみです。
引数に「/」(パスのセパレイター)が使用できなくなっています。
[スクリーンショットの保存]でも保存方法の違いを書いていますが、改めてSDカードのファイル入出力について書いておきます。
テキストファイルの読み書きを例とします。
テキストファイルの読み込み
1 | private void readFile(String filename) { |
3 | FileInputStream inputStream = null ; |
4 | BufferedReader reader = null ; |
7 | inputStream = new FileInputStream(filename); |
8 | reader = new BufferedReader( |
9 | new InputStreamReader(inputStream)); |
11 | while ((line = reader.readLine()) != null ) { |
16 | } catch (IOException e) { |
18 | } catch (SQLException e) { |
20 | } catch (Exception e) { |
26 | } catch (IOException e) { |
テキストファイルの書き込み
1 | private void writeFile(String filename, String src){ |
3 | FileOutputStream outputStream = new FileOutputStream(filename); |
4 | BufferedWriter writer = new BufferedWriter( |
5 | new OutputStreamWriter(outputStream)); |
9 | } catch (FileNotFoundException e) { |
11 | } catch (IOException e) { |