执行程序时,我的程序崩溃了。
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;
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 sendEmail() {
DatabaseHelper dbhelper = new DatabaseHelper(getApplicationContext());
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
myInternalFile = new File(directory , filename);
try {
FileOutputStream fOut = new FileOutputStream(myInternalFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor curCSV = db.rawQuery("SELECT * FROM Leads ", null);
while (curCSV.moveToNext()) {
String arrStr[] = {curCSV.getString(0), curCSV.getString(1)};
Log.e("firstsql",arrStr[1]);
Log.e("firstsql1",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);
}
String[] TO = {""};
String[] CC = {""};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
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());
Log.e("ppppp", myInternalFile.getName());
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
final Uri uri = FileProvider.getUriForFile(this, "com.example.renu.customlead", directory);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
Uri uri = Uri.parse("content://com.example.renu.customlead/files/MySampleFile.txt");
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;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
emailIntent.putExtra(Intent.EXTRA_TEXT, myData);
try {
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();
}
}
}
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="external_files" path="app_MyFileStorage/" />
</paths>
Android Manifest:
.........
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.renu.customlead"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
..........
我的错误代码显示:
03-08 01:45:55.705 7479-7479/com.example.renu.customlead E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.renu.customlead, PID: 7479
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
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:185)
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)