Skip to content

Commit bcdc8d6

Browse files
committed
Add Internal Storage Example
1 parent eb58b7b commit bcdc8d6

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed

Diff for: 3.AndroidFragments/AndroidFragment/.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,86 @@
11
package com.example.storage.internal.fouliex.androidinternalstorage;
22

3+
import android.content.Context;
34
import android.support.v7.app.AppCompatActivity;
45
import android.os.Bundle;
6+
import android.view.View;
7+
import android.widget.Button;
8+
import android.widget.EditText;
9+
import android.widget.Toast;
10+
11+
import java.io.BufferedReader;
12+
import java.io.FileNotFoundException;
13+
import java.io.FileOutputStream;
14+
import java.io.IOException;
15+
import java.io.InputStreamReader;
516

617
public class MainActivity extends AppCompatActivity {
18+
EditText editTextFileName, editTextData;
19+
Button saveButton, readButton;
720

821
@Override
922
protected void onCreate(Bundle savedInstanceState) {
1023
super.onCreate(savedInstanceState);
1124
setContentView(R.layout.activity_main);
25+
26+
editTextFileName = (EditText) findViewById(R.id.editText1);
27+
editTextData = (EditText) findViewById(R.id.editText2);
28+
saveButton = (Button) findViewById(R.id.button1);
29+
readButton = (Button) findViewById(R.id.button2);
30+
31+
//Performing Action on Read Button
32+
performActionOnSave();
33+
//Performing Action on Save Button
34+
performActionOnRead();
35+
36+
}
37+
38+
private void performActionOnRead() {
39+
readButton.setOnClickListener(new View.OnClickListener() {
40+
@Override
41+
public void onClick(View v) {
42+
String filename = editTextFileName.getText().toString();
43+
StringBuffer stringBuffer = new StringBuffer();
44+
45+
try {
46+
BufferedReader inputReader = new BufferedReader(new InputStreamReader(openFileInput(filename)));
47+
String inputString;
48+
while ((inputString = inputReader.readLine()) != null) {
49+
stringBuffer.append(inputString + "\n");
50+
}
51+
} catch (FileNotFoundException e) {
52+
e.printStackTrace();
53+
} catch (IOException e) {
54+
e.printStackTrace();
55+
}
56+
Toast.makeText(getApplicationContext(), stringBuffer.toString(),
57+
Toast.LENGTH_LONG).show();
58+
59+
}
60+
});
61+
}
62+
63+
void performActionOnSave() {
64+
saveButton.setOnClickListener(new View.OnClickListener() {
65+
@Override
66+
public void onClick(View v) {
67+
String filename = editTextFileName.getText().toString();
68+
String data = editTextData.getText().toString();
69+
70+
FileOutputStream fos;
71+
72+
try {
73+
fos = openFileOutput(filename, Context.MODE_PRIVATE);
74+
fos.write(data.getBytes());
75+
fos.close();
76+
77+
Toast.makeText(getApplicationContext(), filename + " saved", Toast.LENGTH_LONG).show();
78+
} catch (FileNotFoundException e) {
79+
e.printStackTrace();
80+
} catch (IOException e) {
81+
e.printStackTrace();
82+
}
83+
}
84+
});
1285
}
1386
}

Diff for: 7.AndroidStorage/AndroidInternalStorage/app/src/main/res/layout/activity_main.xml

+46-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,53 @@
1010
android:paddingTop="@dimen/activity_vertical_margin"
1111
tools:context="com.example.storage.internal.fouliex.androidinternalstorage.MainActivity">
1212

13+
<EditText
14+
android:id="@+id/editText1"
15+
android:layout_width="wrap_content"
16+
android:layout_height="wrap_content"
17+
android:layout_alignParentRight="true"
18+
android:layout_alignParentTop="true"
19+
android:layout_marginRight="20dp"
20+
android:layout_marginTop="24dp"
21+
android:ems="10">
22+
23+
<requestFocus />
24+
</EditText>
25+
26+
<EditText
27+
android:id="@+id/editText2"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:layout_alignRight="@+id/editText1"
31+
android:layout_below="@id/editText1"
32+
android:layout_marginTop="24dp"
33+
android:ems="10" />
34+
1335
<TextView
36+
android:id="@+id/textView1"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:layout_alignBaseline="@+id/editText1"
40+
android:layout_alignBottom="@id/editText1"
41+
android:layout_alignParentLeft="true"
42+
android:text="File Name:" />
43+
44+
<Button
45+
android:id="@+id/button1"
46+
android:layout_width="wrap_content"
47+
android:layout_height="wrap_content"
48+
android:layout_alignLeft="@id/editText2"
49+
android:layout_below="@id/editText2"
50+
android:layout_marginLeft="70dp"
51+
android:layout_marginTop="16dp"
52+
android:text="save" />
53+
54+
<Button
55+
android:id="@+id/button2"
1456
android:layout_width="wrap_content"
1557
android:layout_height="wrap_content"
16-
android:text="Hello World!" />
58+
android:layout_alignBaseline="@+id/button1"
59+
android:layout_alignBottom="@id/button1"
60+
android:layout_toRightOf="@id/button1"
61+
android:text="read" />
1762
</RelativeLayout>

0 commit comments

Comments
 (0)