-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
executable file
·112 lines (94 loc) · 3.89 KB
/
MainActivity.java
File metadata and controls
executable file
·112 lines (94 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package net.learn2develop.externalstorage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
public class MainActivity extends Activity {
static final int READ_BLOCK_SIZE = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//---writing to files---
try
{
if (IsExternalStorageAvailableAndWriteable()) {
//---external Storage---
File extStorage =
getExternalFilesDir(null);
File file = new File(extStorage, "textfile.txt");
FileOutputStream fOut = new
FileOutputStream(file);
OutputStreamWriter osw = new
OutputStreamWriter(fOut);
//---write the string to the file---
osw.write("The quick brown fox jumps over the lazy dog");
osw.flush();
osw.close();
//---display file saved message---
Toast.makeText(getBaseContext(),
"File saved successfully!",
Toast.LENGTH_SHORT).show();
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
//---reading from files---
try
{
if (IsExternalStorageAvailableAndWriteable()) {
//---External Storage---
File extStorage =
getExternalFilesDir(null);
File file = new File(extStorage, "textfile.txt");
FileInputStream fIn = new FileInputStream(file);
InputStreamReader isr = new
InputStreamReader(fIn);
char[] inputBuffer = new char[READ_BLOCK_SIZE];
String s = "";
int charRead;
while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString =
String.copyValueOf(inputBuffer, 0,
charRead);
s += readString;
inputBuffer = new char[READ_BLOCK_SIZE];
}
isr.close();
Toast.makeText(getBaseContext(),
"File loaded successfully! " + s,
Toast.LENGTH_SHORT).show();
}
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
public boolean IsExternalStorageAvailableAndWriteable() {
boolean externalStorageAvailable = false;
boolean externalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
//---you can read and write the media---
externalStorageAvailable = externalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
//---you can only read the media---
externalStorageAvailable = true;
externalStorageWriteable = false;
} else {
//---you cannot read nor write the media---
externalStorageAvailable = externalStorageWriteable = false;
}
return externalStorageAvailable && externalStorageWriteable;
}
}