External Storage in Android 10
In Android 10 the Google restricted external storage but had also provided a solution to store files such as doc, image, text etc. Just go through the blog & learn steps to use external storage in Android 10. This will also work for below versions of android but in Android versions from 11 and above it is restricted for now. So the trick is to use requestLegacyExternalStorage=”true” in application tag in manifest file and it allow application to store files in external store.
Step 1: Add permission in manifest.xml &
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uktechians.storageinandroid">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.StorageInANdroid">
<activity android:name=".StoreFileExternalActivity" /> <provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
<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>
Add xml directory under res and create provider_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
<root-path name="external_files" path="/storage/" />
</paths>
Let’s see how to create & write text in file, now let’s create views in xml file activity_store_file_external.xml
<?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=".StoreFileInternalActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="External Storage"
android:layout_marginTop="40dp"
android:textSize="30sp"
android:textStyle="bold"
android:id="@+id/title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:layout_width="match_parent"
android:layout_height="200dp"
android:hint="Write Something"
android:textSize="20sp"
android:gravity="start"
android:padding="5dp"
android:layout_margin="20dp"
android:id="@+id/ed_add_something"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/title" />
<Button
android:id="@+id/btn_write"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:padding="10dp"
android:text="Write"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/ed_add_something" />
<Button
android:id="@+id/btn_read"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:padding="10dp"
android:text="Read"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="@id/btn_write"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/ed_add_something" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now StoreFileExternalActivity.java we will add code here that will write file in external store also add permission.
package com.demo.storageinandroid;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class StoreFileExternalActivity extends AppCompatActivity implements View.OnClickListener {
Button btn_write, btn_read;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store_file_external);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)==
PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)==
PackageManager.PERMISSION_GRANTED){
Log.d("External Storage", "Permission Granted");
}
else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 120);
}
init();
}
private void init() {
btn_write = (Button) findViewById(R.id.btn_write);
btn_read = (Button) findViewById(R.id.btn_read);
editText = (EditText) findViewById(R.id.ed_add_something);
btn_read.setOnClickListener(this);
btn_write.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId()==R.id.btn_read){
readFile();
}
else if (view.getId()==R.id.btn_write){
writeFile();
}
}
private void readFile() {
StringBuffer stringBuffer = new StringBuffer();
String aDataRow = "";
String aBuffer = "";
try {
File myFile = new File("/sdcard/"+"UKTechians.txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
myReader.close();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),aBuffer,Toast.LENGTH_LONG).show();
}
private void writeFile() {
FileOutputStream fos;
OutputStreamWriter osw;
try {
File file = new File("/sdcard/UKTechians.txt");
file.createNewFile();
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos);
osw.append(editText.getText().toString());
osw.close();
fos.close();
Toast.makeText(getApplicationContext(),"Write file in to External Storage",Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
For more tutorials visit YouTube Channel: https://www.youtube.com/channel/UC7knwauBJLQgs43FuUwHm8A
Also follow us on Facebook: https://www.facebook.com/UKTechiansTutorials