尝试在应用程序内附加 .txt 但失败
Trying to attach .txt inside app but fails
我正在使用 android studio 开发一个 android 移动应用程序,用户可以在其中发送 .txt 作为附件......它正在发送邮件但没有附件
电子邮件代码 .....................
如果需要更改,请告诉我......................
当我尝试发送时没有显示附件 toast .........我认为这意味着我提供了正确的路径
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);
}
生成.txt的代码
生成 .txt 时,它显示路径为 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();
}
GeotagDatabase.txt 不会共享,因为它保存在只能由您的应用程序访问的内部存储中。
https://developer.android.com/guide/topics/data/data-storage.html#filesInternal
您可以将其保存在外部存储区或使用 FileProvider
您无法访问应用程序文件
所以我把文件写在外部存储中
现在我可以将它作为附件发送
代码更改为:
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();
}
我正在使用 android studio 开发一个 android 移动应用程序,用户可以在其中发送 .txt 作为附件......它正在发送邮件但没有附件
电子邮件代码 ..................... 如果需要更改,请告诉我...................... 当我尝试发送时没有显示附件 toast .........我认为这意味着我提供了正确的路径
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);
}
生成.txt的代码
生成 .txt 时,它显示路径为 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();
}
GeotagDatabase.txt 不会共享,因为它保存在只能由您的应用程序访问的内部存储中。
https://developer.android.com/guide/topics/data/data-storage.html#filesInternal
您可以将其保存在外部存储区或使用 FileProvider
您无法访问应用程序文件 所以我把文件写在外部存储中 现在我可以将它作为附件发送 代码更改为:
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();
}