android在外部存储共享目录(storage)创建文件

时间:2024-03-06 18:51:45

MainActivty.java

package com.example.button2;

import android.os.Bundle;

import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.View;

import androidx.core.view.WindowCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.button2.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;
import android.os.Environment;
import android.widget.Button;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button=(Button)findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 检查外部存储是否可用
                if (isExternalStorageWritable()) {
                    createFileInSharedDirectory();
                } else {
                    // 外部存储不可用,处理异常情况
                }
            }
        });

    }



    // 检查外部存储是否可写
    private boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();//得到外部存储状态字符串
        return Environment.MEDIA_MOUNTED.equals(state);// 判断是否可用

    }

    // 在共享目录中创建文件
    private void createFileInSharedDirectory() {
        File sharedDir = null;
        String fileName = "exampleTest.txt";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            sharedDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
            Log.i("path",""+sharedDir);
            sharedDir.mkdirs();    // 创建目录(如果不存在)
            File file = new File(sharedDir, fileName);  // 创建文件
            try {
                // 写入文件内容
                FileWriter writer = new FileWriter(file);
                writer.write("Hello, world!");
                writer.close();
                Log.i("sucess","文件创建成功"+file.getAbsolutePath());
            } catch (IOException e) {
                // 处理异常情况
                e.printStackTrace();
            }
        }
    }






}

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

真机文件存储路径:

/sdcard/Documents/share