试图从PC读取文本文件/从PC写入文本文件。 SD卡访问不起作用。调试器进入file.exists(),然后向BlockGuard交付东西???
Android Studio 3.3.2 版本号AI-182.5107.16.33.5314842,于2019年2月15日建立 JRE:1.8.0_152-release-1248-b01 amd64 JVM:JetBrains s.r.o的OpenJDK 64位服务器VM Windows 10 10.0
代码是:
public int write(String fname, String fcontent){
if ((fcontent == null)||(Objects.equals(fcontent, ""))) return 1; //nothing to write
try {
File file = new File(fname);
// If file does not exists, then create it
try {
if (!file.exists())
file.createNewFile();
} catch (SecurityException e) {
return 2;
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(fcontent);
bw.close();
Log.d("Success","Success");
return 0;
} catch (IOException e) {
if (e.getMessage() == "Permission denied" )
return 3;
else {
e.printStackTrace();
return -1;
}
}
}
我的Android清单现在显示了重复的权限密钥:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="cjg.paycheck">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_label"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:fullBackupContent="@xml/backup_descriptor"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name="cjg.paycheck.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
进一步的研究表明,当sdcard移至PC时,Android保存的sdcard文件不可见;当sdcard放回手机时,Android看不到PC保存的文件。