Skip to content

Instantly share code, notes, and snippets.

@mohosyny
Created July 1, 2020 20:03
Show Gist options
  • Save mohosyny/776744864af86f5960405248e43534c7 to your computer and use it in GitHub Desktop.
Save mohosyny/776744864af86f5960405248e43534c7 to your computer and use it in GitHub Desktop.
first : implementation files('libs/opencsv-2.4.jar')
public class ExportDatabaseCSVTask extends AsyncTask<String, String, String> {
private File file;
private WeakReference<Context> context;
private ProgressDialog dialog;
private boolean isSuccess = true;
private final String[] arrStr1 = {"Name", "column 1", "column 2", "column 3", "column 4", "column 5"};
private String fileName;
public ExportDatabaseCSVTask(Context ctx) {
context = new WeakReference<>(ctx);
Hawk.init(context.get()).build();
}
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(context.get());
this.dialog.setMessage("در حال ساخت فایل...");
this.dialog.show();
}
protected String doInBackground(final String... args) {
makeDir();
writeData();
return "";
}
private void makeDir() {
File exportDir = new File(Constants.FOLDER_ADDRESS);
if (!exportDir.exists()) {
//noinspection ResultOfMethodCallIgnored
exportDir.mkdirs();
}
///// set name and type for export file
fileName = "fileName" + Util.getDate() + ".csv";
file = new File(exportDir, fileName);
}
private void writeData() {
try {
boolean isCreated = file.createNewFile();
if (isCreated) {
CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
List<User> users = AppDatabse.getInstance(context.get()).mUserDao().getAll();
int x = AppDatabse.getInstance(context.get()).mUserDao().countUsers();
csvWrite.writeNext(arrStr1);
// fill columns
for (int y = 0; y < x; y++) {
String[] arrStr = {users.get(y).getUserName(), String.valueOf(users.get(y).getUserMarhale()),
String.valueOf(users.get(y).getTrack1()), String.valueOf(users.get(y).getTrack2()),
String.valueOf(users.get(y).getTrack3()), String.valueOf(users.get(y).getTrack4() };
csvWrite.writeNext(arrStr);
}
csvWrite.close();
}
} catch (IOException e) {
Log.e("Activity", e.getMessage(), e);
}
}
@SuppressLint("NewApi")
@Override
protected void onPostExecute(final String success) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
if (success.isEmpty()) {
showSuccessfulDialog();
} else {
showFailedDialog();
}
}
private void showSuccessfulDialog() {
String msg = "اشتراک فایل :";
setDialog(msg);
isSuccess = true;
}
private void showFailedDialog() {
String msg = "خطایی رخ داده است.";
setDialog(msg);
isSuccess = false;
}
private void setDialog(String message) {
if (!isSuccess) {
new MaterialAlertDialogBuilder(context.get())
.setMessage(message).show();
} else {
new MaterialAlertDialogBuilder(context.get())
.setMessage(message)
.setPositiveButton("بله", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
shareFile2();
}
})
.setNegativeButton("خیر", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}
}
private void shareFile2() {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("application/csv");
sendIntent.putExtra(Intent.EXTRA_EMAIL, "");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "فایل خروجی اپلیکیشن فلان ");
sendIntent.putExtra(Intent.EXTRA_TEXT, "");
String temp_path = Constants.FOLDER_ADDRESS + "/" + fileName;
File F = new File(temp_path);
Uri U = Uri.fromFile(F);
sendIntent.putExtra(Intent.EXTRA_STREAM, U);
context.get().startActivity(Intent.createChooser(sendIntent, "ارسال به :"));
}
}
////////////////////////////////////////////////////////////////////////////////
and in your Activty/Fragment cal this method :
private void exportCSVTask(Context context) {
ExportDatabaseCSVTask task = new ExportDatabaseCSVTask(context);
task.execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment