#======================文件创建实例=====================
#代码有红色报错部分:win按键+回车键引用包
1.安卓模拟器允许权限:设置》Apps》找到自己开发的软件名称》Permissions》勾选允许
2.查看是否创建成功:
CMD命令进入
cd C:\android-sdk\platform-tools
adb shell
#运行
ls /storage/emulated/0
查看文件
3.代码添加权限(添加内容):
prokect/app/src/mian/AndroidManifest
?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.administrator.myapplication"> <!--文件读写权限--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!--文件系统挂载--> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.INTERNET"/> <!--网络权限-->
#===========================
运行运行程序目录
project/app/src/main/java/com.example/MainA
package com.example.administrator.myapplication; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import java.io.File; import java.io.IOException; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ Log.e("SD卡公开根目录,与程序无关",Environment.getExternalStorageDirectory().getAbsolutePath()); Log.e("SD卡公开目录,与程序有关",getExternalFilesDir(null).getAbsolutePath()); Log.e("手机自带目录,与程序有关",getFilesDir().getAbsolutePath()); // 输出SD卡下公开目录”Music”的路径信息 // 获取SD卡下Music目录 File file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC); Log.e("SD卡下公开目录Music,与程序无关",file.getAbsolutePath()); // 在SD卡根目录下新建Swift目录 // 获取SD卡根目录 File rootFile=Environment.getExternalStorageDirectory(); File swiftFile=new File(rootFile,"Swift"); swiftFile.mkdir(); // 在SD卡已有的目录Documents下新建Swift.txt文件; if (!documentFile.exists())documentFile.mkdir(); 强制创建 File documentFile=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS); if (!documentFile.exists())documentFile.mkdir(); File swiftFile2=new File(documentFile,"Swift.txt"); try { swiftFile2.createNewFile(); } catch (IOException e) { e.printStackTrace(); } // 4.在SD卡对应程序的目录下新建test.txt文件 File packageFile=getExternalFilesDir(null); File swiftDir=new File(packageFile,"test.txt"); try { swiftDir.createNewFile(); } catch (IOException e) { e.printStackTrace(); } // 在手机存储对应程序下新建swift/test.txt文件。 File file3=getFilesDir(); File swiftDir2=new File(file3,"swift/test.txt"); try { swiftDir2.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } }
提示报错修改文件,将11改成19;位置:project>app>build.gradle
apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "27.0.3" defaultConfig { applicationId "com.example.administrator.myapplication" minSdkVersion 19 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" }
#=====================
Powered by ddoss.cn 12.0
©2015 - 2024 ddoss
渝公网安备50011302222260号 渝ICP备2024035333号 【实验平台安全承诺书】 小绿叶技术社区,优化网络中,点击查看配置信息
您的IP:192.168.122.82,2024-10-10 19:58:17,Processed in 0.02303 second(s).