我正在尝试实现一种方法来保存和读取android Apk中的对象数组,但是它不起作用。我没有任何编译错误。我需要知道这个错误。
数组用于保存参数以使用微控制器Arduino。这是用C ++编写的,程序将通过蓝牙读取bin文件。此外,微控制器的程序通过蓝牙保存。代码如下
@RequiresApi(api = Build.VERSION_CODES.N)
public void saveData(Context context, int option){
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"@filepaths/public_dir");
Uri fileUri;
File fileprivate = new File("@filepaths/private_dir");
try {
fileUri = FileProvider.getUriForFile(
context,
"com.sistemaderiegoandroid_arduino",
file);
} catch (Exception e) {
ResumenFragment.errorMsg(e.toString());
};
try{
if (!file.mkdirs()) {
file.mkdir();
};}
catch(Exception e){ResumenFragment.errorMsg(e.toString());
};
ObjectOutputStream serializatorDataR=null;
ObjectOutputStream serializatorDataT=null;
ObjectOutputStream serializatorDataL=null;
ObjectOutputStream serializatorDataH=null;
FileOutputStream arduinoDataR=null;
FileOutputStream arduinoDataT=null;
FileOutputStream arduinoDataH=null;
FileOutputStream arduinoDataL=null;
FileDescriptor fdR=null;
FileDescriptor fdT=null;
FileDescriptor fdH=null;
FileDescriptor fdL=null;
File fileNameR = null;
File fileNameT = null;
File fileNameH = null;
File fileNameL = null;
try {
switch (option) {
case 1:
fileNameR = new File(file, "arduino_data_resumen.bin");
fileNameT = new File(file, "arduino_data_tiempo.bin");
fileNameH = new File(file, "arduino_data_humedad.bin");
fileNameL = new File(file, "arduino_data_luz.bin");
break;
case 2:
fileNameR = new File(fileprivate, "arduino_data_resumen.bin");
fileNameT = new File(fileprivate, "arduino_data_tiempo.bin");
fileNameH = new File(fileprivate, "arduino_data_humedad.bin");
fileNameL = new File(fileprivate, "arduino_data_luz.bin");
break;
default:
fileNameR = new File(fileprivate, "arduino_data_resumen.bin");
fileNameT = new File(fileprivate, "arduino_data_tiempo.bin");
fileNameH = new File(fileprivate, "arduino_data_humedad.bin");
fileNameL = new File(fileprivate, "arduino_data_luz.bin");
break;
};}catch(Exception e){ResumenFragment.errorMsg(e.toString());
};
try {
fileNameR.delete();
arduinoDataR = new FileOutputStream(fileNameR,true);
fdR = arduinoDataR.getFD();
serializatorDataR = new ObjectOutputStream(arduinoDataR);
for (int i = 0; i < MainActivity.allData.arrayDatosResumen.length; i++) {
serializatorDataR.writeObject(MainActivity.allData.arrayDatosResumen[i]);
serializatorDataR.flush();
fdR.sync();
}
;
serializatorDataR.close();
fileNameT.delete();
arduinoDataT = new FileOutputStream(fileNameT, true);
fdT = arduinoDataT.getFD();
serializatorDataT = new ObjectOutputStream(arduinoDataT);
for (int i = 0; i < MainActivity.allData.arrayDatosTiempo.length; i++) {
serializatorDataT.writeObject(MainActivity.allData.arrayDatosTiempo[i]);
serializatorDataT.flush();
fdT.sync();
};
serializatorDataT.close();
fileNameH.delete();
arduinoDataH = new FileOutputStream(fileNameH, true);
fdH = arduinoDataH.getFD();
serializatorDataH = new ObjectOutputStream(arduinoDataH);
for (int i = 0; i < MainActivity.allData.arrayDatosHumedad.length; i++) {
serializatorDataH.writeObject(MainActivity.allData.arrayDatosHumedad[i]);
serializatorDataH.flush();
fdH.sync();
}
;
serializatorDataH.close();
fileNameL.delete();
arduinoDataL = new FileOutputStream(fileNameL);
fdL = arduinoDataL.getFD();
serializatorDataL = new ObjectOutputStream(arduinoDataL);
for (int i = 0; i < MainActivity.allData.arrayDatosLuz.length; i++) {
serializatorDataL.writeObject(MainActivity.allData.arrayDatosLuz[i]);
serializatorDataL.flush();
fdL.sync();
}
;
serializatorDataL.close();
ResumenFragment.toastsave();
}
catch(Exception e){ResumenFragment.errorMsg(e.toString());
}
finally {
if (serializatorDataR != null) {
try {
serializatorDataR.close();
} catch (IOException e) {
ResumenFragment.errorMsg(e.toString());
}
};
if (arduinoDataR != null) {
try {
arduinoDataR.close();
} catch (IOException e) {
ResumenFragment.errorMsg(e.toString());
}
};
if (serializatorDataT != null) {
try {
serializatorDataR.close();
} catch (IOException e) {
ResumenFragment.errorMsg(e.toString());
}
};
if (arduinoDataT != null) {
try {
arduinoDataR.close();
} catch (IOException e) {
ResumenFragment.errorMsg(e.toString());
}
};
if (serializatorDataH != null) {
try {
serializatorDataR.close();
} catch (IOException e) {
ResumenFragment.errorMsg(e.toString());
}
};
if (arduinoDataH != null) {
try {
arduinoDataR.close();
} catch (Exception e) {
ResumenFragment.errorMsg(e.toString());
}
};
if (serializatorDataL != null) {
try {
serializatorDataR.close();
} catch (Exception e) {
ResumenFragment.errorMsg(e.toString());
}
};
if (arduinoDataL != null) {
try {
arduinoDataR.close();
} catch (Exception e) {
ResumenFragment.errorMsg(e.toString());
}
};
};
};
@RequiresApi(api = Build.VERSION_CODES.N)
public void loadData(Context context, int option){
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"@filepaths/public_dir");
Uri fileUri;
File fileprivate = new File("@filepaths/private_dir");
try {
fileUri = FileProvider.getUriForFile(
context,
"com.sistemaderiegoandroid_arduino",
file);
} catch (Exception e) {
ResumenFragment.errorMsg(e.toString());
};
ObjectInputStream serializatorDataR=null;
ObjectInputStream serializatorDataT=null;
ObjectInputStream serializatorDataL=null;
ObjectInputStream serializatorDataH=null;
FileInputStream arduinoDataR=null;
FileInputStream arduinoDataT=null;
FileInputStream arduinoDataH=null;
FileInputStream arduinoDataL=null;
FileDescriptor fdR=null;
FileDescriptor fdT=null;
FileDescriptor fdH=null;
FileDescriptor fdL=null;
File fileNameR = null;
File fileNameT = null;
File fileNameH = null;
File fileNameL = null;
try {
switch (option) {
case 1:
fileNameR = new File(file, "arduino_data_resumen.bin");
fileNameT = new File(file, "arduino_data_tiempo.bin");
fileNameH = new File(file, "arduino_data_humedad.bin");
fileNameL = new File(file, "arduino_data_luz.bin");
break;
case 2:
fileNameR = new File(fileprivate, "arduino_data_resumen.bin");
fileNameT = new File(fileprivate, "arduino_data_tiempo.bin");
fileNameH = new File(fileprivate, "arduino_data_humedad.bin");
fileNameL = new File(fileprivate, "arduino_data_luz.bin");
break;
default:
fileNameR = new File(fileprivate, "arduino_data_resumen.bin");
fileNameT = new File(fileprivate, "arduino_data_tiempo.bin");
fileNameH = new File(fileprivate, "arduino_data_humedad.bin");
fileNameL = new File(fileprivate, "arduino_data_luz.bin");
break;
};}catch(Exception e){ResumenFragment.errorMsg(e.toString());
};
try {
arduinoDataR = new FileInputStream(fileNameR);
fdR = arduinoDataR.getFD();
serializatorDataR = new ObjectInputStream(arduinoDataR);
DatosArduinoR [] objectR = new DatosArduinoR[6];
objectR = (DatosArduinoR[]) serializatorDataR.readObject();
fdR.sync();
for (int i = 0; i < 6; i++) {
if (objectR != null) {
MainActivity.allData.arrayDatosResumen[i] = objectR[i];
}else {MainActivity.allData.arrayDatosResumen[i] = null;}
;
}
;
serializatorDataR.close();
objectR = null;
arduinoDataT = new FileInputStream(fileNameT);
fdT = arduinoDataT.getFD();
serializatorDataT = new ObjectInputStream(arduinoDataT);
DatosArduinoT [] objectT = new DatosArduinoT[6];
objectT = (DatosArduinoT[]) serializatorDataT.readObject();
fdT.sync();
for (int i = 0; i < 6; i++) {
if (objectT != null) {
MainActivity.allData.arrayDatosTiempo[i] = objectT[i];
}else {MainActivity.allData.arrayDatosTiempo[i] = null;}
;
}
;
serializatorDataT.close();
arduinoDataH = new FileInputStream(fileNameH);
fdH = arduinoDataH.getFD();
serializatorDataH = new ObjectInputStream(arduinoDataH);
DatosArduinoH [] objectH = new DatosArduinoH[6];
objectH = (DatosArduinoH[]) serializatorDataH.readObject();
fdH.sync();
for (int i = 0; i < 6; i++) {
if (objectH != null) {
MainActivity.allData.arrayDatosHumedad[i] = objectH[i];
}else {MainActivity.allData.arrayDatosHumedad[i] = null;}
;
}
;
serializatorDataH.close();
objectH = null;
arduinoDataL = new FileInputStream(fileNameL);
fdL = arduinoDataL.getFD();
serializatorDataL = new ObjectInputStream(arduinoDataL);
DatosArduinoL [] objectL = new DatosArduinoL[6];
objectL = (DatosArduinoL[]) serializatorDataL.readObject();
fdL.sync();
for (int i = 0; i < 6; i++) {
if (objectL != null) {
MainActivity.allData.arrayDatosLuz[i] = objectL[i];
}else {MainActivity.allData.arrayDatosLuz[i] = null;}
;
}
;
serializatorDataL.close();
objectL = null;
ResumenFragment.toastsave();
}
catch(Exception e){ResumenFragment.errorMsg(e.toString());
}
finally {
if (serializatorDataR != null) {
try {
serializatorDataR.close();
} catch (IOException e) {
ResumenFragment.errorMsg(e.toString());
}
};
if (arduinoDataR != null) {
try {
arduinoDataR.close();
} catch (IOException e) {
ResumenFragment.errorMsg(e.toString());
}
};
if (serializatorDataT != null) {
try {
serializatorDataR.close();
} catch (IOException e) {
ResumenFragment.errorMsg(e.toString());
}
};
if (arduinoDataT != null) {
try {
arduinoDataR.close();
} catch (IOException e) {
ResumenFragment.errorMsg(e.toString());
}
};
if (serializatorDataH != null) {
try {
serializatorDataR.close();
} catch (IOException e) {
ResumenFragment.errorMsg(e.toString());
}
};
if (arduinoDataH != null) {
try {
arduinoDataR.close();
} catch (Exception e) {
ResumenFragment.errorMsg(e.toString());
}
};
if (serializatorDataL != null) {
try {
serializatorDataR.close();
} catch (Exception e) {
ResumenFragment.errorMsg(e.toString());
}
};
if (arduinoDataL != null) {
try {
arduinoDataR.close();
} catch (Exception e) {
ResumenFragment.errorMsg(e.toString());
}
};
};
};
完整的软件包在这里 https://1drv.ms/u/s!Asr3Ewn45KOLtGOqDkoMq20A-hbc?e=VoDBAj