package com.vimal.recreat; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import android.Manifest; import android.content.pm.PackageManager; import android.os.Bundle; import android.app.Activity; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; public class MainActivity extends Activity { EditText inputText; TextView response; Button saveButton,readButton; private String filename = "3333.txt"; String myfolder = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS); private String filepath = myfolder ; File myExternalFile; String myData = ""; private static final int PERMISSION_REQUEST = 101; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); inputText = (EditText) findViewById(R.id.myInputText); response = (TextView) findViewById(R.id.response); saveButton = (Button) findViewById(R.id.saveExternalStorage); saveButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { GetPErmission(); } }); readButton = (Button) findViewById(R.id.getExternalStorage); readButton.setOnClickListener(new OnClickListener() { @Override 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..."); } }); if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) { saveButton.setEnabled(false); } else { myExternalFile = new File(getExternalFilesDir(filepath), filename); } } private static boolean isExternalStorageReadOnly() { String extStorageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) { return true; } return false; } private static boolean isExternalStorageAvailable() { String extStorageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(extStorageState)) { return true; } return false; } public void DoExportFile(){ 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..."); } public void GetPErmission() { int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); if (permissionCheck == PackageManager.PERMISSION_GRANTED) { DoExportFile(); } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST); } } public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == PERMISSION_REQUEST) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { DoExportFile(); } else { Toast.makeText(this, "You don't have required permission to Store File", Toast.LENGTH_SHORT).show(); } } } }
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Reading and Writing to External Storage" android:textSize="24sp"/> <EditText android:id="@+id/myInputText" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:lines="5" android:minLines="3" android:gravity="top|left" android:inputType="textMultiLine"> <requestFocus /> </EditText> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="1.0" android:layout_marginTop="20dp"> <Button android:id="@+id/saveExternalStorage" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="SAVE" android:layout_weight="0.5"/> <Button android:id="@+id/getExternalStorage" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.5" android:text="READ" /> </LinearLayout> <TextView android:id="@+id/response" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:text="" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>
myfolder
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)