package com.example.renu.customlead;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Calendar;
/**
* Created by RENU on 2/4/2018.
*/
public class MainLandingPage extends AppCompatActivity {
private String filename = "MySampleFile.txt";
private String filepath = "MyFileStorage";
File myInternalFile;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void AddLead(View view) {
DatabaseHelper dbhelper = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor curCSV = db.rawQuery("SELECT * FROM Leads ", null);
int n=curCSV.getCount();
if (n >= 1) {
Intent intent = new Intent(this, MainActivity3.class);
startActivity(intent);
}
else{
Toast.makeText(MainLandingPage.this, "No Field has been declared", Toast.LENGTH_SHORT).show();
}
}
public void SendCsv(View view) {
sendEmail();
}
public void DeleteRecord(View view) {
DatabaseHelper dbhelper = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor curCSV = db.rawQuery("SELECT * FROM Leads where id <> 1", null);
int n = curCSV.getCount();
if (n > 1) {
deleteRecordALL();
Toast.makeText(MainLandingPage.this, "Leads Deleted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainLandingPage.this, "No Leads to be Deleted", Toast.LENGTH_SHORT).show();
}
}
public void DeleteAll(View view) {
DatabaseHelper dbhelper = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor curCSV = db.rawQuery("SELECT * FROM Leads ", null);
int n=curCSV.getCount();
if (n > 1) {
deleteRecordALL();
Toast.makeText(MainLandingPage.this, "Leads Deleted", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainLandingPage.this, "No Leads to be Deleted", Toast.LENGTH_SHORT).show();
}
}
protected void deleteRecordALL() {
DatabaseHelper dbhelper = new DatabaseHelper(getApplicationContext());
//SQLiteDatabase db = dbhelper.getReadableDatabase();
//db1.execSQL("delete from Leads");
Log.d("Delete: ", "Deleting ..");
dbhelper.deleteRec();
}
public void createField(View view){
Intent intenti = new Intent(MainLandingPage.this, MainActivity.class);
startActivity(intenti);
}
// export sqlite content to csv
// private void exportDB() {
//
//
// }
protected void sendEmail() {
//File dbFile = getDatabasePath("LeadsManager.db"); /storage/emulated/0
DatabaseHelper dbhelper = new DatabaseHelper(getApplicationContext());
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
myInternalFile = new File(directory , filename);
//File file = new File(Environment.getExternalStorageDirectory() + File.separator + "data.csv");
/*File mfolder = new File(getFilesDir().getPath());
File file = new File("/storage/emulated/0/Android/data/com.example.renu.customlead/files" + "/data.csv");
if (!mfolder.exists()) {
mfolder.mkdirs();
}
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.e("Main file name", file.getName());
Log.e("Internalpath",getCacheDir().getPath());
Log.e("Path",getFilesDir().getPath());
Log.e("fileexists", String.valueOf(file.exists()));
Log.e("Path",Environment.getExternalStorageDirectory().getPath());
*/
try {
//file.createNewFile();
//CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
FileOutputStream fOut = new FileOutputStream(myInternalFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor curCSV = db.rawQuery("SELECT * FROM Leads ", null);
//myOutWriter.writeNext(curCSV.getColumnNames());
while (curCSV.moveToNext()) {
//Which column you want to exprort
String arrStr[] = {curCSV.getString(0), curCSV.getString(1)};
Log.e("firstsql",arrStr[1]);
Log.e("firstsql1",arrStr[0]);
//myOutWriter.append(arrStr[0]);
myOutWriter.append(arrStr[1]);
myOutWriter.append("\n");
}
myOutWriter.close();
curCSV.close();
Log.e("Main", String.valueOf(myInternalFile.length()));
} catch (Exception sqlEx) {
Log.e("MainActivity", sqlEx.getMessage(), sqlEx);
}
Log.i("Send email", "");
String[] TO = {""};
String[] CC = {""};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Lead created as of " + Calendar.getInstance().getTime());
emailIntent.putExtra(Intent.EXTRA_TEXT, myInternalFile.getAbsolutePath());
// emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myInternalFile));
Log.e("ppppp", myInternalFile.getName());
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// emailIntent.setType("text/*");
Uri uri = FileProvider.getUriForFile(MainLandingPage.this, "com.example.renu.customlead",myInternalFile);
//data/user/0/com.example.renu.customlead/app_MyFileStorage/MySampleFile.txt
//com.example.renu.customlead/files/MySampleFile.txt
//Uri uri = Uri.parse("content://com.example.renu.customlead/files/MySampleFile.txt");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
Log.e("xxyyxx", myInternalFile.getName());
Log.e("xxyyxxx", myInternalFile.getPath());
Log.e("xxyyxxxx", myInternalFile.getAbsoluteFile().getName());
String myData="";
try {
FileInputStream fis = new FileInputStream(myInternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br =
new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + "\n" + strLine;
}
Log.e("aaaaaaaaaaaaaaa",myData);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
emailIntent.putExtra(Intent.EXTRA_TEXT, myData);
try {
//startActivity(Intent.createChooser(emailIntent, "Send mail..."));
startActivityForResult(Intent.createChooser(emailIntent,"Sending mail Done "),1);
finish();
Log.i("Finished sending email", "email xxxx");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainLandingPage.this, "There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}
}
程序因以下错误而失败
03-07 11:50:24.524 27630-27630/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.renu.customlead, PID: 27630
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6261)
at android.widget.TextView.performClick(TextView.java:11159)
at android.view.View$PerformClick.run(View.java:23748)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6261)
at android.widget.TextView.performClick(TextView.java:11159)
at android.view.View$PerformClick.run(View.java:23748)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.renu.customlead/app_MyFileStorage/MySampleFile.txt
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
at com.example.renu.customlead.MainLandingPage.sendEmail(MainLandingPage.java:182)
at com.example.renu.customlead.MainLandingPage.SendCsv(MainLandingPage.java:61)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6261)
at android.widget.TextView.performClick(TextView.java:11159)
at android.view.View$PerformClick.run(View.java:23748)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)