Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách tạo file txt trong bộ nhớ ngoài với quyền runtime trong Android


Ví dụ này minh họa cách tạo file txt trong bộ nhớ ngoài (external storage) với quyền runtime trên Android.

Bước 1: Tạo dự án mới

Tạo dự án mới trong Android Studio: File → New Project, điền thông tin cần thiết.

Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
    xmlns:tools = "https://schemas.android.com/tools"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    tools:context = ".MainActivity"
    android:orientation = "vertical">
    <EditText
        android:id = "@+id/enterText"
        android:hint = "Nhập văn bản tại đây"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content" />
    <Button
        android:id = "@+id/save"
        android:text = "Lưu"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content" />
</LinearLayout>

Giao diện có EditText và nút Lưu. Khi nhấn nút, dữ liệu từ EditText sẽ được lưu vào bộ nhớ ngoài.

Bước 3: Code MainActivity.java

package com.example.andy.myapplication;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private static final int PERMISSION_REQUEST_CODE = 100;
    Button save;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText enterText = findViewById(R.id.enterText);
        save = findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!enterText.getText().toString().isEmpty()) {
                    String state = Environment.getExternalStorageState();
                    if (Environment.MEDIA_MOUNTED.equals(state)) {
                        if (Build.VERSION.SDK_INT >= 23) {
                            if (checkPermission()) {
                                saveTextToExternalStorage(enterText.getText().toString());
                            } else {
                                requestPermission();
                            }
                        } else {
                            saveTextToExternalStorage(enterText.getText().toString());
                        }
                    }
                }
            }
        });
    }

    private void saveTextToExternalStorage(String text) {
        File sdcard = Environment.getExternalStorageDirectory();
        File dir = new File(sdcard.getAbsolutePath() + "/text/");
        dir.mkdir(); // Tạo thư mục nếu chưa có
        File file = new File(dir, "sample.txt");
        try {
            FileOutputStream os = new FileOutputStream(file);
            os.write(text.getBytes());
            os.close();
            Toast.makeText(this, "Đã lưu file text", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private boolean checkPermission() {
        int result = ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
        return result == PackageManager.PERMISSION_GRANTED;
    }

    private void requestPermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            Toast.makeText(MainActivity.this, "Quyền ghi bộ nhớ ngoài cho phép tạo file. Vui lòng cấp quyền trong Cài đặt.", Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.e("value", "Đã cấp quyền, giờ có thể ghi bộ nhớ ngoài.");
                } else {
                    Log.e("value", "Quyền bị từ chối, không thể ghi bộ nhớ ngoài.");
                }
                break;
        }
    }
}

Bước 4: Cấu hình AndroidManifest.xml

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "https://schemas.android.com/apk/res/android"
    package = "com.example.andy.myapplication">
    <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: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>

Chạy ứng dụng

Kết nối thiết bị, chạy từ Android Studio. Màn hình hiển thị giao diện mặc định.

Cách tạo file txt trong bộ nhớ ngoài với quyền runtime trong Android

Nhập văn bản và nhấn Lưu như hình dưới:

Cách tạo file txt trong bộ nhớ ngoài với quyền runtime trong Android

Lần đầu sẽ yêu cầu runtime permission truy cập bộ nhớ ngoài. Để kiểm tra, mở bộ nhớ ngoài và tìm thư mục text như hình dưới:

Cách tạo file txt trong bộ nhớ ngoài với quyền runtime trong Android