I'm developing an android mobile application using android studio, where an user can send .txt as attachment ........It is sending mail but with no attachment
Email code ............. If changes are required kindly tell me............ No attachment toast is not shown when i try to send .........I think this means that i am providing correct path
public void Send(View view){
Uri docUri=Uri.parse("android.resource://com.example.shaz.geotag/files/GeotagDatabase.txt");
intent =new Intent(Intent.ACTION_SEND);
if(docUri!=null){
intent.putExtra(Intent.EXTRA_STREAM,docUri);}
else {
Toast.makeText(this, "No attachment", Toast.LENGTH_LONG).show();
}
intent.setType("message/rfc822");
intent.setData(Uri.parse("mailto:"));
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,"Attached GeotagDatabase.txt");
chooser=Intent.createChooser(intent,"Sending Email....");
startActivity(chooser);
}
Code where .txt is generated
When .txt is generated it shows path as txt/data/user/0/com.example.shaz.geotag/files/GeotagDatabase.txt
public String Attach()
{
SQLiteDatabase db =this.getWritableDatabase();
String[] columns={CoL_1, CoL_2, CoL_3};
Cursor cursor;
cursor = db.query(TABLE_NAME,columns,null,null,null,null,null);
StringBuffer stringBuffer= new StringBuffer();
File file =null;
FileOutputStream fileOutputStream=null;
while (cursor.moveToNext()){
int index1= cursor.getColumnIndex(CoL_1);
int index2= cursor.getColumnIndex(CoL_2);
int index3= cursor.getColumnIndex(CoL_3);
int id =cursor.getInt(index1);
String latitude =cursor.getString(index2);
String longitude =cursor.getString(index3);
try{
file=c.getFilesDir();
fileOutputStream = c.openFileOutput("GeotagDatabase.txt", Context.MODE_PRIVATE);
String text1= Integer.toString(id)+" ";
String text2=latitude+" ";
String text3=longitude+"\n";
fileOutputStream.write(text1.getBytes());
fileOutputStream.write(text2.getBytes());
fileOutputStream.write(text3.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(c, "ADDED to txt"+file+"/GeotagDatabase.txt", Toast.LENGTH_LONG).show();
stringBuffer.append(id+" "+latitude+" "+longitude+"\n");
}
return stringBuffer.toString();
}
You cannot access app files So I wrote the file in external storage Now i can send it as attachment Changes in Code are:
public String Attach()
{
SQLiteDatabase db =this.getWritableDatabase();
String[] columns={CoL_1, CoL_2, CoL_3};
Cursor cursor;
cursor = db.query(TABLE_NAME,columns,null,null,null,null,null);
StringBuffer stringBuffer= new StringBuffer();
fileName= myFile+"." +"txt";
// writing file on external storage
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/Files");
dir.mkdirs();
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
new File(dir, children[i]).delete();
}
}
file = new File(dir, fileName);
}
catch (Exception e){}
while (cursor.moveToNext()) {
int index1 = cursor.getColumnIndex(CoL_1);
int index2 = cursor.getColumnIndex(CoL_2);
int index3 = cursor.getColumnIndex(CoL_3);
int id = cursor.getInt(index1);
String latitude = cursor.getString(index2);
String longitude = cursor.getString(index3);
try {
fileOutputStream = new FileOutputStream(file);
fpath = file.toString();
String text1 = Integer.toString(id) + " ";
String text2 = latitude + " ";
String text3 = longitude + "\n";
fileOutputStream.write(text1.getBytes());
fileOutputStream.write(text2.getBytes());
fileOutputStream.write(text3.getBytes());
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(c, "Add to" + fpath, Toast.LENGTH_LONG).show();
stringBuffer.append(id + " " + latitude + " " + longitude + "\n");
}
return stringBuffer.toString();
}