我已经编写了SLIDENERD教程中的代码,以使用以下代码将信息保存在内部缓存,外部缓存,私人信息和公共信息等不同区域。
EditText uname,pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname=(EditText)findViewById(R.id.editText);
pwd=(EditText)findViewById(R.id.editText3);
}
public void intCache(View view){
File fileDir=getCacheDir();
File myFile=new File(fileDir,"User_info_intCache.txt");
write(myFile);
}
public void extCache(View view){
File fileDir=getExternalCacheDir();
File myFile=new File(fileDir,"User_info_extCache.txt");
write(myFile);
}
public void pvtDir(View view){
File fileDir=getExternalFilesDir("User_Info");
File myFile=new File(fileDir,"User_info_pvtExt.txt");
write(myFile);
}
public void pubDir(View view){
File fileDir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File myFile=new File(fileDir,"User_info_pubExt.txt");
write(myFile);
}
private void write(File myFile){
String unameS=uname.getText().toString();
String pwdS=pwd.getText().toString();
FileOutputStream fileOutputStream=null;
try {
fileOutputStream=new FileOutputStream(myFile);
try {
fileOutputStream.write(unameS.getBytes());
fileOutputStream.write(pwdS.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
Toast.makeText(this,"data is written to "+myFile.getAbsolutePath(),Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我可以看到文件user_info_extCache.txt,User_info_pvtExt.txt。我知道内部缓存位置(User_info_intCache.txt)无法看到。但是我找不到必须存储在公共目录Downloads.I的User_info_pubExt.txt文件。我无法看到通过以下代码创建的文件。
File fileDir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File myFile=new File(fileDir,"User_info_pubExt.txt");
toast显示该文件是由/storage/emulated/0/Download/User_info_pubExt.txt下的上面一段代码创建的。
我发现在某个地方,如果手机连接到电脑,外部存储器无法访问。所以我尝试通过从PC上移除手机。吐司说文件已创建。但我无法在该文件夹下找到它。
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.iamka.storage">
<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>
<activity android:name=".Main2Activity"></activity>
</application>
答案 0 :(得分:0)
声称数据写入的吐司位置错误。由于吐司也会在出现异常时显示。
将吐司放在try块中。或者在close()之后更好,但只有你关闭()。
并在catch块中放置其他toast以通知用户e.getMessage()
。
至于异常:所有路径都执行最后一个路径根本不需要任何读/写权限。对于后者,您需要读/写权限。
但是对于Android 6+而言,在清单中请求它们是不够的,因为您必须添加一些代码来要求用户确认所请求的权限。
Google获取运行时权限。