Created
November 28, 2020 05:28
-
-
Save edward1986/7ae8c952427f3a126975cc833b7f3bac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout 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" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<EditText | |
android:id="@+id/editText" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:ems="10" | |
android:inputType="textMultiLine" | |
app:layout_constraintBottom_toTopOf="@+id/buttonRead" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<Button | |
android:id="@+id/buttonRead" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:onClick="readFile" | |
android:text="Read" | |
app:layout_constraintBottom_toTopOf="@+id/buttonWrite" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" /> | |
<Button | |
android:id="@+id/buttonWrite" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:onClick="writeFile" | |
android:text="Write" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.externalstoragefile"> | |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.externalstoragefile; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.core.app.ActivityCompat; | |
import android.Manifest; | |
import android.content.pm.PackageManager; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.view.View; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.InputStreamReader; | |
public class MainActivity extends AppCompatActivity { | |
private final String FILENAME="testfile.txt"; | |
private final String[] PERMISSIONS_STORAGE = { | |
Manifest.permission.READ_EXTERNAL_STORAGE, | |
Manifest.permission.WRITE_EXTERNAL_STORAGE | |
}; | |
EditText mEditText; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mEditText = findViewById(R.id.editText); | |
} | |
public boolean isExternalStorageWritable() { | |
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { | |
return true; | |
} | |
return false; | |
} | |
public boolean isExternalStorageReadable() { | |
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || | |
Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState())) { | |
return true; | |
} | |
return false; | |
} | |
public void checkStoragePermission() { | |
int permission = ActivityCompat.checkSelfPermission(this, | |
Manifest.permission.WRITE_EXTERNAL_STORAGE); | |
if (permission != PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, 101); | |
} | |
} | |
public void writeFile(View view) { | |
if (isExternalStorageWritable()) { | |
checkStoragePermission(); | |
try { | |
File textFile = new File(Environment.getExternalStorageDirectory(), FILENAME); | |
FileOutputStream fileOutputStream = new FileOutputStream(textFile); | |
fileOutputStream.write(mEditText.getText().toString().getBytes()); | |
fileOutputStream.close(); | |
} catch (java.io.IOException e) { | |
e.printStackTrace(); | |
Toast.makeText(this, "Error writing file", Toast.LENGTH_LONG).show(); | |
} | |
} else { | |
Toast.makeText(this, "Cannot write to External Storage", Toast.LENGTH_LONG).show(); | |
} | |
} | |
public void readFile(View view) { | |
if (isExternalStorageReadable()) { | |
checkStoragePermission(); | |
StringBuilder stringBuilder = new StringBuilder(); | |
try { | |
File textFile = new File(Environment.getExternalStorageDirectory(), FILENAME); | |
FileInputStream fileInputStream = new FileInputStream(textFile); | |
if (fileInputStream != null) { | |
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); | |
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); | |
String newLine = null; | |
while ((newLine = bufferedReader.readLine()) != null) { | |
stringBuilder.append(newLine + "\n"); | |
} | |
fileInputStream.close(); | |
} | |
mEditText.setText(stringBuilder); | |
} catch (java.io.IOException e) { | |
e.printStackTrace(); | |
Toast.makeText(this, "Error reading file", Toast.LENGTH_LONG).show(); | |
} | |
} else { | |
Toast.makeText(this, "Cannot read External Storage", | |
Toast.LENGTH_LONG).show(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment