boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
this code return false and should true. I can't create new files beacuse of that.
I added <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> and <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> to manifest file
Code:
response = (TextView) findViewById(R.id.text_from_file);
inputText = (EditText) findViewById(R.id.edittext_sum);
readButton = (Button) findViewById(R.id.read_from_file);
saveButton = (Button) findViewById(R.id.add_sum);
readButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
FileInputStream fis = new FileInputStream(myExternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
inputText.setText(myData);
response.setText("SampleFile.txt data retrieved from Internal Storage...");
}
});
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
FileOutputStream fos = new FileOutputStream(myExternalFile);
fos.write(inputText.getText().toString().getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
inputText.setText("");
response.setText("SampleFile.txt saved to External Storage...");
}
});
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
saveButton.setEnabled(false);
System.out.println("Available: "+isExternalStorageAvailable()+", ReadOnly: "+isExternalStorageReadOnly());
}
else {
myExternalFile = new File(getExternalFilesDir(filepath), filename);
}
I tried all methods from stackoverflow Nothing helped. Android emulator external memory = 200mb.
Edited by ligblitz, 08 January 2018 - 11:10 AM.