android studio 开发环境下,在sd卡创建个文本文件,花了一天也没有搞定,请高手指点。 权限
<uses-permission android:name="android.permission.INTERNET"/> //访问网络的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> //写sdcard权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> //读sdcard权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> //访问本地文件权限
sd卡建个文本文件,代码:
String str = null;
boolean sdCardExist = Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
if (sdCardExist)
{
File sdDir = Environment.getExternalStorageDirectory();//获取根目录
mytext1.setText("sd卡存在 "+sdDir); //设置文本框内容
str = Environment.getExternalStorageDirectory().getAbsolutePath(); //获取绝对路径到字符串
mytext2.setText("sd卡存在 "+str); //设置文本框内容
} //创建一个文件,保存用户名和密码
File myfile = new File(str + "Android/data/sh.sh01/files/info.txt");
try {
FileOutputStream fos = new FileOutputStream(myfile);
//写入用户名和密码,以name##passwd的格式写入
fos.write(("sshh" + "##" + "shsh123").getBytes());
//关闭输出流
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
<uses-permission android:name="android.permission.INTERNET"/> //访问网络的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> //写sdcard权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> //读sdcard权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> //访问本地文件权限
sd卡建个文本文件,代码:
String str = null;
boolean sdCardExist = Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
if (sdCardExist)
{
File sdDir = Environment.getExternalStorageDirectory();//获取根目录
mytext1.setText("sd卡存在 "+sdDir); //设置文本框内容
str = Environment.getExternalStorageDirectory().getAbsolutePath(); //获取绝对路径到字符串
mytext2.setText("sd卡存在 "+str); //设置文本框内容
} //创建一个文件,保存用户名和密码
File myfile = new File(str + "Android/data/sh.sh01/files/info.txt");
try {
FileOutputStream fos = new FileOutputStream(myfile);
//写入用户名和密码,以name##passwd的格式写入
fos.write(("sshh" + "##" + "shsh123").getBytes());
//关闭输出流
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
compileSdkVersion 24
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "sh.sh01"
minSdkVersion 22
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
//compile 'com.lzy.net:okgo:3.0.4'
//compile files('libs/okhttp-2.7.5.jar')
//compile files('libs/okio-1.14.0.jar')
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okio:okio:1.7.0'
}我的好像不6.0啊