一、新建一个empty activity的项目
二、修改AndroidMainfest.xml,添加用户权限。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.aimee.test1"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <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>
三、修改activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginTop="8dp" android:text="CreateFile" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
四、修改MainActivity.java
package com.example.aimee.test1; import android.Manifest; import android.content.pm.PackageManager; import android.os.Build; import android.os.Environment; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; import java.io.File; import java.io.IOException; public class MainActivity extends AppCompatActivity { private final int REQUESTCODE=101; private String fileName="test"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { create(fileName); } catch (IOException e) { e.printStackTrace(); } } }); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults){ super.onRequestPermissionsResult(requestCode,permissions,grantResults); if(requestCode==REQUESTCODE){ if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE) && grantResults[0]== PackageManager.PERMISSION_GRANTED){ Toast.makeText(MainActivity.this,"111",Toast.LENGTH_LONG).show(); }else { Toast.makeText(MainActivity.this,"222",Toast.LENGTH_LONG).show(); } } } public void create(String fileName) throws IOException { if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){ int checkSelfPermission=checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE); if(checkSelfPermission==PackageManager.PERMISSION_DENIED){ requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUESTCODE); } } String path=Environment.getExternalStorageDirectory().getAbsolutePath(); File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+fileName); if(!file.exists()){ boolean isSuccess=file.mkdirs(); Toast.makeText(MainActivity.this,"333",Toast.LENGTH_LONG).show(); }else{ String file_name=path+"/test.txt"; File f=new File(file_name); if(!f.exists()){ Toast.makeText(MainActivity.this,"文件不存在",Toast.LENGTH_LONG).show(); f.createNewFile(); } else{ Toast.makeText(MainActivity.this,"文件创建成功",Toast.LENGTH_LONG).show(); } Toast.makeText(MainActivity.this,path,Toast.LENGTH_LONG).show(); } } }
如此便可以创建一个文件夹test,在手机的根目录下,同时创建一个text.txt的文件,在test文件夹下。