Last active
December 11, 2021 05:43
-
-
Save dmitryweiner/b27b2741ae58a047f587fedeba0c6755 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.myapplication; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.core.app.ActivityCompat; | |
import androidx.core.content.ContextCompat; | |
import android.Manifest; | |
import android.content.pm.PackageManager; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.view.View; | |
import android.widget.Toast; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
/** | |
* src\main\AndroidManifest.xml | |
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
* <application android:requestLegacyExternalStorage="true" ...></application> | |
*/ | |
public class MainActivity extends AppCompatActivity { | |
int PERMISSION_REQUEST_CODE = 1; | |
String fileName = "content.txt"; | |
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), fileName); | |
String text = "Test string"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
public void onWithoutPermissions(View view) { | |
writeToFile(); | |
} | |
public void onWithPermissions(View view) { | |
if (checkPermission()) { | |
writeToFile(); | |
} else { | |
requestPermission(); | |
} | |
} | |
private void requestPermission() { | |
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { | |
Toast.makeText(this, "Permissions should be granted", Toast.LENGTH_LONG).show(); | |
} else { | |
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE); | |
} | |
} | |
private boolean checkPermission() { | |
int result = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE); | |
if (result == PackageManager.PERMISSION_GRANTED) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) | |
{ | |
super.onRequestPermissionsResult(requestCode, | |
permissions, | |
grantResults); | |
if (requestCode == PERMISSION_REQUEST_CODE) { | |
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
Toast.makeText(MainActivity.this, "Permission granted", Toast.LENGTH_SHORT) .show(); | |
writeToFile(); | |
} | |
else { | |
Toast.makeText(MainActivity.this, "Permission denied", Toast.LENGTH_SHORT) .show(); | |
} | |
} | |
} | |
private void writeToFile() { | |
try | |
{ | |
file.createNewFile(); | |
FileOutputStream fos = new FileOutputStream(file); | |
fos.write(text.getBytes()); | |
fos.close(); | |
Toast.makeText(this, "Текстовый файл успешно сохранён!", Toast.LENGTH_SHORT).show(); | |
} catch (FileNotFoundException e) | |
{ | |
e.printStackTrace(); | |
Toast.makeText(this, "Файл не найден! " + e.getMessage(), Toast.LENGTH_SHORT).show(); | |
} catch (IOException e) | |
{ | |
e.printStackTrace(); | |
Toast.makeText(this, "Ошибка сохранения файла! " + e.getMessage(), Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment