kopia lustrzana https://github.com/SP8EBC/MeteoSystem
exporting data to file
rodzic
f903a5e636
commit
7577673df6
|
@ -58,6 +58,5 @@ dependencies {
|
||||||
implementation 'org.apache.poi:poi:3.12'
|
implementation 'org.apache.poi:poi:3.12'
|
||||||
// implementation 'org.apache.poi:poi-ooxml:3.12'
|
// implementation 'org.apache.poi:poi-ooxml:3.12'
|
||||||
implementation 'com.fasterxml:aalto-xml:1.0.0'
|
implementation 'com.fasterxml:aalto-xml:1.0.0'
|
||||||
implementation 'net.rdrei.android.dirchooser:library:3.2@aar'
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,8 +1,14 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
package="cc.pogoda.mobile.pogodacc">
|
package="cc.pogoda.mobile.pogodacc">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||||
|
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"
|
||||||
|
tools:ignore="ProtectedPermissions" />
|
||||||
|
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
|
|
|
@ -4,8 +4,12 @@ import androidx.annotation.Nullable;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.UriPermission;
|
||||||
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.provider.DocumentsContract;
|
||||||
|
import android.provider.MediaStore;
|
||||||
import android.text.Editable;
|
import android.text.Editable;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
|
@ -21,6 +25,12 @@ import org.greenrobot.eventbus.ThreadMode;
|
||||||
import org.threeten.bp.ZoneId;
|
import org.threeten.bp.ZoneId;
|
||||||
import org.threeten.bp.ZonedDateTime;
|
import org.threeten.bp.ZonedDateTime;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import cc.pogoda.mobile.pogodacc.R;
|
import cc.pogoda.mobile.pogodacc.R;
|
||||||
import cc.pogoda.mobile.pogodacc.dao.StationDataDao;
|
import cc.pogoda.mobile.pogodacc.dao.StationDataDao;
|
||||||
import cc.pogoda.mobile.pogodacc.file.ExcelExport;
|
import cc.pogoda.mobile.pogodacc.file.ExcelExport;
|
||||||
|
@ -35,7 +45,7 @@ public class ExportDataActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private Button selectStationButton;
|
private Button selectStationButton;
|
||||||
|
|
||||||
private Button startExportButton;
|
private Button startExportButton, outputFileButton;
|
||||||
|
|
||||||
private Spinner outputFormat;
|
private Spinner outputFormat;
|
||||||
|
|
||||||
|
@ -49,19 +59,33 @@ public class ExportDataActivity extends AppCompatActivity {
|
||||||
|
|
||||||
WeatherStation stationToExport = null;
|
WeatherStation stationToExport = null;
|
||||||
|
|
||||||
|
Uri exportUri;
|
||||||
|
|
||||||
public WeatherStation getStationToExport() {
|
public WeatherStation getStationToExport() {
|
||||||
return stationToExport;
|
return stationToExport;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void openDirectory(Uri uriToLoad) {
|
public void openDirectory(Uri uriToLoad) {
|
||||||
// Choose a directory using the system's file picker.
|
// // Choose a directory using the system's file picker.
|
||||||
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
|
// Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
|
||||||
|
//
|
||||||
|
// // Optionally, specify a URI for the directory that should be opened in
|
||||||
|
// // the system file picker when it loads.
|
||||||
|
// //intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uriToLoad);
|
||||||
|
//
|
||||||
|
// act.startActivityForResult(intent, 123);
|
||||||
|
|
||||||
|
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
||||||
|
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||||
|
intent.setType("application/vnd.ms-excel");
|
||||||
|
intent.putExtra(Intent.EXTRA_TITLE, "text.xls");
|
||||||
|
|
||||||
// Optionally, specify a URI for the directory that should be opened in
|
// Optionally, specify a URI for the directory that should be opened in
|
||||||
// the system file picker when it loads.
|
// the system file picker when your app creates the document.
|
||||||
//intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uriToLoad);
|
//intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);
|
||||||
|
|
||||||
|
startActivityForResult(intent, 123);
|
||||||
|
|
||||||
act.startActivityForResult(intent, 123);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getExportLnInHours() {
|
public int getExportLnInHours() {
|
||||||
|
@ -108,7 +132,7 @@ public class ExportDataActivity extends AppCompatActivity {
|
||||||
|
|
||||||
switch (selected) {
|
switch (selected) {
|
||||||
case "CSV" : return 1;
|
case "CSV" : return 1;
|
||||||
case "MS Excel XLSX": return 2;
|
case "MS Excel XLS": return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -117,17 +141,30 @@ public class ExportDataActivity extends AppCompatActivity {
|
||||||
@Override
|
@Override
|
||||||
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||||
|
|
||||||
String s;
|
|
||||||
|
|
||||||
if (requestCode == 123 && resultCode == RESULT_OK) {
|
if (requestCode == 123 && resultCode == RESULT_OK) {
|
||||||
Uri uri = data.getData();
|
Uri uri = data.getData();
|
||||||
|
|
||||||
String decoded = Uri.decode(uri.toString());
|
grantUriPermission(getPackageName(), uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
|
||||||
|
getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
|
||||||
|
|
||||||
s = uri.getPath();
|
exportUri = uri;
|
||||||
|
|
||||||
System.out.println(decoded);
|
// try {
|
||||||
System.out.println(s);
|
//
|
||||||
|
// OutputStream out = getContentResolver().openOutputStream(uri);
|
||||||
|
//
|
||||||
|
// PrintWriter writer = new PrintWriter(out);
|
||||||
|
//
|
||||||
|
// writer.write("dupajasia");
|
||||||
|
//
|
||||||
|
// writer.flush();
|
||||||
|
// writer.close();
|
||||||
|
// out.close();
|
||||||
|
// } catch (FileNotFoundException e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,6 +189,15 @@ public class ExportDataActivity extends AppCompatActivity {
|
||||||
exportLn = findViewById(R.id.editTextNumberExport);
|
exportLn = findViewById(R.id.editTextNumberExport);
|
||||||
datePicker = findViewById(R.id.datePickerExportStartDate);
|
datePicker = findViewById(R.id.datePickerExportStartDate);
|
||||||
outputFormat = findViewById(R.id.spinnerOutputFormat);
|
outputFormat = findViewById(R.id.spinnerOutputFormat);
|
||||||
|
outputFileButton = findViewById(R.id.buttonExportTarget);
|
||||||
|
|
||||||
|
outputFileButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View view) {
|
||||||
|
openDirectory(null);
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
selectStationButton.setOnClickListener(new View.OnClickListener() {
|
selectStationButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
|
||||||
|
@ -187,10 +233,12 @@ public class ExportDataActivity extends AppCompatActivity {
|
||||||
|
|
||||||
int format = act.getOutputFormat();
|
int format = act.getOutputFormat();
|
||||||
|
|
||||||
openDirectory(null);
|
|
||||||
|
|
||||||
if (format == 2) {
|
if (format == 2) {
|
||||||
ExcelExport.exportToExcel(stationData, toExport, act.getApplicationContext());
|
try {
|
||||||
|
ExcelExport.exportToExcel(stationData, toExport, act.getApplicationContext(), getContentResolver().openOutputStream(exportUri));
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import cc.pogoda.mobile.pogodacc.type.WeatherStation;
|
import cc.pogoda.mobile.pogodacc.type.WeatherStation;
|
||||||
import cc.pogoda.mobile.pogodacc.type.web.ListOfStationData;
|
import cc.pogoda.mobile.pogodacc.type.web.ListOfStationData;
|
||||||
|
@ -22,7 +23,11 @@ import cc.pogoda.mobile.pogodacc.type.web.ListOfStationData;
|
||||||
public class ExcelExport {
|
public class ExcelExport {
|
||||||
|
|
||||||
|
|
||||||
public static boolean exportToExcel(ListOfStationData data, WeatherStation station, Context context) {
|
public static boolean exportToExcel(ListOfStationData data, WeatherStation station, Context context, OutputStream out) {
|
||||||
|
|
||||||
|
if (out == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
Cell cell;
|
Cell cell;
|
||||||
|
|
||||||
|
@ -56,13 +61,12 @@ public class ExcelExport {
|
||||||
cell = header.createCell(7);
|
cell = header.createCell(7);
|
||||||
cell.setCellValue("Wind Gusts");
|
cell.setCellValue("Wind Gusts");
|
||||||
|
|
||||||
File file = new File(context.getExternalFilesDir(null), "test.xls");
|
|
||||||
FileOutputStream fileOutputStream = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fileOutputStream = new FileOutputStream(file);
|
workbook.write(out);
|
||||||
|
|
||||||
workbook.write(fileOutputStream);
|
out.flush();
|
||||||
|
out.close();
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
android:layout_width="362dp"
|
android:layout_width="362dp"
|
||||||
android:layout_height="47dp"
|
android:layout_height="47dp"
|
||||||
android:layout_marginStart="28dp"
|
android:layout_marginStart="28dp"
|
||||||
android:layout_marginTop="44dp"
|
android:layout_marginTop="32dp"
|
||||||
android:layout_marginEnd="21dp"
|
android:layout_marginEnd="21dp"
|
||||||
android:backgroundTint="#56039BE5"
|
android:backgroundTint="#56039BE5"
|
||||||
android:text="@string/select_station_export"
|
android:text="@string/select_station_export"
|
||||||
|
@ -25,9 +25,7 @@
|
||||||
android:id="@+id/textViewStationToExport"
|
android:id="@+id/textViewStationToExport"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginStart="175dp"
|
android:layout_marginTop="8dp"
|
||||||
android:layout_marginTop="14dp"
|
|
||||||
android:layout_marginEnd="178dp"
|
|
||||||
android:fontFamily="@font/alegreya_sans_sc_medium"
|
android:fontFamily="@font/alegreya_sans_sc_medium"
|
||||||
android:text="@string/dash"
|
android:text="@string/dash"
|
||||||
android:textAllCaps="true"
|
android:textAllCaps="true"
|
||||||
|
@ -40,23 +38,23 @@
|
||||||
android:id="@+id/spinnerOutputFormat"
|
android:id="@+id/spinnerOutputFormat"
|
||||||
android:layout_width="247dp"
|
android:layout_width="247dp"
|
||||||
android:layout_height="29dp"
|
android:layout_height="29dp"
|
||||||
android:layout_marginTop="52dp"
|
android:layout_marginTop="8dp"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/textViewStationToExport" />
|
app:layout_constraintTop_toBottomOf="@+id/textView" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/textView"
|
android:id="@+id/textView"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="64dp"
|
android:layout_marginTop="16dp"
|
||||||
android:fontFamily="@font/alegreya_sans_sc_medium"
|
android:fontFamily="@font/alegreya_sans_sc_medium"
|
||||||
android:text="@string/output_export_format"
|
android:text="@string/output_export_format"
|
||||||
android:textSize="20sp"
|
android:textSize="20sp"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintHorizontal_bias="0.498"
|
app:layout_constraintHorizontal_bias="0.498"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/buttonSelectStationExport" />
|
app:layout_constraintTop_toBottomOf="@+id/textViewStationToExport" />
|
||||||
|
|
||||||
|
|
||||||
<DatePicker
|
<DatePicker
|
||||||
|
@ -77,12 +75,11 @@
|
||||||
android:id="@+id/textView2"
|
android:id="@+id/textView2"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="12dp"
|
android:layout_marginTop="16dp"
|
||||||
android:fontFamily="@font/alegreya_sans_sc_medium"
|
android:fontFamily="@font/alegreya_sans_sc_medium"
|
||||||
android:text="@string/start_data"
|
android:text="@string/export_from"
|
||||||
android:textSize="20sp"
|
android:textSize="20sp"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintHorizontal_bias="0.497"
|
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/spinnerOutputFormat" />
|
app:layout_constraintTop_toBottomOf="@+id/spinnerOutputFormat" />
|
||||||
|
|
||||||
|
@ -90,7 +87,7 @@
|
||||||
android:id="@+id/editTextNumberExport"
|
android:id="@+id/editTextNumberExport"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="12dp"
|
android:layout_marginTop="8dp"
|
||||||
android:ems="10"
|
android:ems="10"
|
||||||
android:inputType="number"
|
android:inputType="number"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
@ -102,7 +99,7 @@
|
||||||
android:id="@+id/textView3"
|
android:id="@+id/textView3"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="22dp"
|
android:layout_marginTop="16dp"
|
||||||
android:fontFamily="@font/alegreya_sans_sc_medium"
|
android:fontFamily="@font/alegreya_sans_sc_medium"
|
||||||
android:text="@string/export_ln"
|
android:text="@string/export_ln"
|
||||||
android:textSize="20sp"
|
android:textSize="20sp"
|
||||||
|
@ -125,10 +122,22 @@
|
||||||
android:id="@+id/buttonExportStart"
|
android:id="@+id/buttonExportStart"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="9dp"
|
|
||||||
android:backgroundTint="#56039BE5"
|
android:backgroundTint="#56039BE5"
|
||||||
android:text="@string/start_export"
|
android:text="@string/start_export"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintHorizontal_bias="0.498"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/buttonExportTarget" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/buttonExportTarget"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:backgroundTint="#56039BE5"
|
||||||
|
android:text="@string/output_fn"
|
||||||
|
app:iconTint="#56039BE5"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/editTextNumberExport" />
|
app:layout_constraintTop_toBottomOf="@+id/editTextNumberExport" />
|
||||||
|
|
||||||
|
|
|
@ -121,7 +121,7 @@
|
||||||
<string name="holfuy_id" translatable="false">Holfuy ID</string>
|
<string name="holfuy_id" translatable="false">Holfuy ID</string>
|
||||||
<string name="email" translatable="false">Email</string>
|
<string name="email" translatable="false">Email</string>
|
||||||
<string name="csv" translatable="false">CSV</string>
|
<string name="csv" translatable="false">CSV</string>
|
||||||
<string name="xlsx" translatable="false">MS Excel XLSX</string>
|
<string name="xlsx" translatable="false">MS Excel XLS</string>
|
||||||
<string-array name="export_formats">
|
<string-array name="export_formats">
|
||||||
<item>@string/email</item>
|
<item>@string/email</item>
|
||||||
<item>@string/csv</item>
|
<item>@string/csv</item>
|
||||||
|
|
Ładowanie…
Reference in New Issue