现在已经停留了一段时间,没有任何线索。我正在尝试编写一些对象以归档正在制作的应用程序,但请继续找不到文件。这是Write&阅读我正在使用的。
我得到的日志要说读是开始的,但是在那之后,我得到了错误,没有更多的日志。
private void loadData() throws IOException, ClassNotFoundException {
// Will run and load all data from the config file into the app's memory
Log.d("READ:", "reading starting");
FileInputStream fileInputStream = new FileInputStream("config");
ObjectInputStream oIS = new ObjectInputStream(fileInputStream);
Object[] output = new Object[4];
for (int i=0; i<4; i++) {
output[i] = oIS.readObject();
Log.d("READ:", output[i].toString());
}
oIS.close();
fileInputStream.close();
return;
}
public void writeData() throws IOException {
FileOutputStream fOut = openFileOutput("config", Context.MODE_PRIVATE);
ObjectOutputStream oOut = new ObjectOutputStream(fOut);
Log.d("writeData:", "streamsOpened");
oOut.writeInt(this.targetINR);
oOut.writeObject(this.inrReadings);
oOut.writeObject(this.weeklyDoses);
oOut.writeObject(this.alarmTime);
Log.d("writeData:", "objectsWritten");
oOut.close();
fOut.close();
Log.d("writeData:", "Streams closed, write finished");
return;
}
引起了称为这些方法的代码。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
inrReadings = new HashMap<Calendar, Integer>();
weeklyDoses = new HashMap<Calendar, Integer>();
TextView debug = (TextView) findViewById(R.id.textView) ;
debug.setText("Vars init");
targetINR = 2;
inrReadings.put(Calendar.getInstance(), 2);
weeklyDoses.put(Calendar.getInstance(), 2);
alarmTime = Calendar.getInstance();;
isStoragePermissionGranted();
/* try {
writeData();
} catch (IOException e) {
e.printStackTrace();
debug.setText(debug.getText() + " errorWrite");
Log.d("writeData:", "ioException");
}
try {
loadData();
} catch (IOException e) {
e.printStackTrace();
Log.d("readData:", "ioException");
debug.setText(debug.getText() + " errorRead");
} catch (ClassNotFoundException e) {
e.printStackTrace();
Log.d("readData:", "ioException");
debug.setText(debug.getText() + " errorClassNotFound");
}
*/ }
这是两个权限检查方法:
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v("Perms:","Permission is granted now");
return true;
} else {
Log.v("Perms:","Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v("Perms:","Permission is granted already");
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
Log.v("Perms:","Permission: "+permissions[0]+ "was "+grantResults[0]);
// User has granted the access. Now you can initiate the file writing.
try {
writeData();
} catch (IOException e) {
e.printStackTrace();
Log.v("Write:", "ioError");
}
MediaScannerConnection.scanFile(this,
new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
}
});
try {
loadData();
} catch (IOException e) {
e.printStackTrace();
Log.v("Read:", "ioError");
} catch (ClassNotFoundException e) {
e.printStackTrace();
Log.v("Read:", "ClassNotFound");
}
}
}
完整错误日志在这里:https://pastebin.com/dhxnxyug
新错误日志:https://pastebin.com/nq0kh1au
**编辑:
用工作修复了它,请参阅答案。**
我认为您尚未在AndroidManifest.xml
文件中添加以下权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
如果您在设备中使用了最新版本的Android SDK,则还必须请求用户许可。这是您可以要求从用户写出外部存储的权限。
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
// User has granted the access. Now you can initiate the file writing.
writeData();
}
}
update
我可以看到您正在尝试在创建文件后立即访问该文件。如果这样做,则需要在访问新创建的文件之前扫描文件。
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
}
});
我认为您需要像以下内容那样修改writeData
功能。
public void writeData() throws IOException {
FileOutputStream fOut = openFileOutput("config", Context.MODE_PRIVATE);
ObjectOutputStream oOut = new ObjectOutputStream(fOut);
Log.d("writeData:", "streamsOpened");
oOut.writeInt(this.targetINR);
oOut.writeObject(this.inrReadings);
oOut.writeObject(this.weeklyDoses);
oOut.writeObject(this.alarmTime);
Log.d("writeData:", "objectsWritten");
oOut.close();
fOut.close();
Log.d("writeData:", "Streams closed, write finished");
MediaScannerConnection.scanFile(this,
new String[]{"/data/data/" + "com.example.yourpackage" + "/config" }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
}
});
return;
}
和loadData
功能应该是。
private void loadData() throws IOException, ClassNotFoundException {
// Will run and load all data from the config file into the app's memory
Log.d("READ:", "reading starting");
FileInputStream fileInputStream = new FileInputStream("/data/data/" + "com.example.yourpackage" + "/config");
ObjectInputStream oIS = new ObjectInputStream(fileInputStream);
Object[] output = new Object[4];
for (int i=0; i<4; i++) {
output[i] = oIS.readObject();
Log.d("READ:", output[i].toString());
}
oIS.close();
fileInputStream.close();
return;
}
将com.example.yourpackage
替换为您的项目的软件包名称。
i最后修复了它,而无需实现onrequestpermision((等。我只是通过仅写一个对象(一个对象(来简化对象写入。该数组在其中存储了所有需要的值。有点了,但它简化了写作过程!