Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]ストレージの章 #56

Open
wants to merge 16 commits into
base: storage
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added articles/images/storage-01/inputstream.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/storage-01/lesson1-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/storage-01/lesson1-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/storage-01/lesson2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/storage-01/outputstream.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/storage-01/streams.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/storage-02/lesson2-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/storage-02/rdbms.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/storage-02/sqlite1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/storage-02/sqlite2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/storage-02/sqlite3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/storage-02/sqlite4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/storage-02/sqlite5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/storage-02/sqlite6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added articles/images/storage-02/tablerowcolumn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
379 changes: 379 additions & 0 deletions articles/storage-01.re

Large diffs are not rendered by default.

581 changes: 581 additions & 0 deletions articles/storage-02.re

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions code/storage/FileSample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
.idea/*
*.iml

1 change: 1 addition & 0 deletions code/storage/FileSample/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
26 changes: 26 additions & 0 deletions code/storage/FileSample/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion "21.0.2"

defaultConfig {
applicationId "jp.androidopentextbook.storage.filesample"
minSdkVersion 10
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.2'
compile 'com.jakewharton:butterknife:6.0.0'
}
17 changes: 17 additions & 0 deletions code/storage/FileSample/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/fkm/software/android-sdk-macosx/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package jp.androidopentextbook.storage.filesample;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
23 changes: 23 additions & 0 deletions code/storage/FileSample/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jp.androidopentextbook.storage.filesample" >

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package jp.androidopentextbook.storage.filesample;

import android.content.Context;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


public class MainActivity extends ActionBarActivity {

@InjectView(R.id.edit)
EditText mEdit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ButterKnife.inject(this, this);
}

@OnClick(R.id.button_internal_load)
void internalLoadClicked() {
InputStream in = null;
InputStreamReader sr = null;
BufferedReader br = null;
try {
in = openFileInput("myText.txt");
sr = new InputStreamReader(in);
br = new BufferedReader(sr);

String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
mEdit.setText(sb.toString());

Toast.makeText(this, R.string.load_done, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Log.e("Internal", "IO Exception " + e.getMessage(), e);
} finally {
try {
if (br != null) { br.close(); }
if (sr != null) { sr.close(); }
if (in != null) { in.close(); }
} catch (IOException e) {
Log.e("Internal", "IO Exception " + e.getMessage(), e);
}
}
}

@OnClick(R.id.button_internal_save)
void internalSaveClicked() {
OutputStream out = null;
OutputStreamWriter writer = null;
BufferedWriter bw = null;
try {
out = openFileOutput("myText.txt", Context.MODE_PRIVATE);
writer = new OutputStreamWriter(out);
bw = new BufferedWriter(writer);

String text = mEdit.getText().toString();
bw.write(text);

Toast.makeText(this, R.string.save_done, Toast.LENGTH_SHORT).show();

} catch (IOException e) {
Log.e("Internal", "IO Exception " + e.getMessage(), e);
} finally {
try {
if (bw != null) { bw.close(); }
if (writer != null) { writer.close(); }
if (out != null) { out.close(); }
} catch (IOException e) {
Log.e("Internal", "IO Exception " + e.getMessage(), e);
}
}
}

@OnClick(R.id.button_external_load)
void externalLoadClicked() {
InputStream in = null;
InputStreamReader sr = null;
BufferedReader br = null;
try {
File foler = Environment.getExternalStoragePublicDirectory("MyDocuments");
File file = new File(foler, "myText.txt");
in = new FileInputStream(file);
sr = new InputStreamReader(in);
br = new BufferedReader(sr);

String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
mEdit.setText(sb.toString());

Toast.makeText(this, R.string.load_done, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Log.e("External", "IO Exception " + e.getMessage(), e);
} finally {
try {
if (br != null) { br.close(); }
if (sr != null) { sr.close(); }
if (in != null) { in.close(); }
} catch (IOException e) {
Log.e("External", "IO Exception " + e.getMessage(), e);
}
}
}

@OnClick(R.id.button_external_save)
void externalSaveClicked() {
OutputStream out = null;
OutputStreamWriter writer = null;
BufferedWriter bw = null;
try {
File foler = Environment.getExternalStoragePublicDirectory("MyDocuments");
if (!foler.exists()) {
boolean result = foler.mkdir();
if (!result) {
return;
}
}
File file = new File(foler, "myText.txt");
out = new FileOutputStream(file);
writer = new OutputStreamWriter(out);
bw = new BufferedWriter(writer);

String text = mEdit.getText().toString();
bw.write(text);

Toast.makeText(this, R.string.save_done, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Log.e("External", "IO Exception " + e.getMessage(), e);
} finally {
try {
if (bw != null) { bw.close(); }
if (writer != null) { writer.close(); }
if (out != null) { out.close(); }
} catch (IOException e) {
Log.e("External", "IO Exception " + e.getMessage(), e);
}
}
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions code/storage/FileSample/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical">

<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:inputType="textMultiLine"
android:gravity="left|top"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/internal_storage"/>
<LinearLayout
android:id="@+id/layout_internal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text_1"
android:orientation="horizontal">
<Button
android:id="@+id/button_internal_load"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/load"/>
<Button
android:id="@+id/button_internal_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/save"/>
</LinearLayout>

<TextView
android:id="@+id/text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/layout_internal"
android:text="@string/external_storage"/>
<LinearLayout
android:id="@+id/layout_extrenal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text_2"
android:orientation="horizontal">
<Button
android:id="@+id/button_external_load"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/load"/>
<Button
android:id="@+id/button_external_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/save"/>
</LinearLayout>

</RelativeLayout>

</LinearLayout>
9 changes: 9 additions & 0 deletions code/storage/FileSample/app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
5 changes: 5 additions & 0 deletions code/storage/FileSample/app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
14 changes: 14 additions & 0 deletions code/storage/FileSample/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">FileSample</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="internal_storage">Internal Storage</string>
<string name="load">Load</string>
<string name="save">Save</string>
<string name="external_storage">External storage</string>
<string name="save_done">Save done!</string>
<string name="load_done">Load done!</string>

</resources>
8 changes: 8 additions & 0 deletions code/storage/FileSample/app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>

</resources>
Loading