Android 发送时文件未附在电子邮件上
我正在将一个数字写入一个文件,并希望将该文件发送到电子邮件。 该文件通过电子邮件接收,但为空 但是当我看到“data/data/com.example.write_file/files/samplefile.txt”时,文件是 好的,我的记录已经写好了 有人知道我做错了什么吗Android 发送时文件未附在电子邮件上,android,Android,我正在将一个数字写入一个文件,并希望将该文件发送到电子邮件。 该文件通过电子邮件接收,但为空 但是当我看到“data/data/com.example.write_file/files/samplefile.txt”时,文件是 好的,我的记录已经写好了 有人知道我做错了什么吗 package com.example.write_file; +import java.io.FileOutputStream; public class MainActivity extends Activity
package com.example.write_file;
+import java.io.FileOutputStream;
public class MainActivity extends Activity {
private EditText getInput;
int D;
String W;
char chr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInput = (EditText) findViewById(R.id.getInput);
D=0;
TextView textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText("Write a number you want to send in the txt file") ;
}
public void onClick(View view) {
write_file();
D=D+1;
W=getInput.getText().toString(); //will get a string
if (getInput.getText().length() > 0){
chr=W.charAt(0);} //W convert to char
switch (view.getId()) {
case R.id.button1:
if (((chr==(char)45)&&(getInput.getText().length() < 2))||(getInput.getText().length() == 0)){
Toast.makeText(this, "Please enter a text",
Toast.LENGTH_LONG).show();
return;
}
TextView getInput1 = (TextView) findViewById(R.id.textView1);
getInput1.setText("Write a number you want to send in the txt file") ;
break; }
}
public void onClick2(View view) {
sendfile();
finish();
}
public void write_file() { //writes into local not to a sdcard
try { // catches IOException below
FileOutputStream fOut = openFileOutput("samplefile.txt",MODE_APPEND);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.append(W + "\n"); // Write the string to the file - "\n" similar to writeln in pascal (use wordpad not notepad)
osw.flush();
osw.close();
if (D>=55) { finish();}
Toast.makeText(this, "I am writing to the file",
Toast.LENGTH_SHORT).show();
// the file you can find in DDMS-File Explorer-"/data/data/your_project_package_structure/files/samplefile.txt"
// there is an icon mobile phone for pulling file from device or putting file onto device
}
catch (IOException ioe)
{Toast.makeText(this, "Exception-file not exists?",
Toast.LENGTH_SHORT).show();}
}
/*
public void sendfile(){
String nfile;
nfile="samplefile.txt";
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.ACTION_SENDTO, new String[]
{"xxxx@yyyy.com"});
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.putExtra(Intent.EXTRA_SUBJECT,
"File attached.");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("data/data/com.example.write_file/files"+ nfile));
startActivity(Intent.createChooser(emailIntent,
"Send email using.."));
Toast.makeText(this, "The file was sent",
Toast.LENGTH_SHORT).show();
} */
public void sendfile(){
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.ACTION_SENDTO, new String[]
{"xxxx@yyyy.com"});
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.putExtra(Intent.EXTRA_SUBJECT,
"File attached.");
File file = getFileStreamPath("samplefile.txt");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+file.getAbsolutePath()));
startActivity(Intent.createChooser(emailIntent,
"Send email using.."));
Toast.makeText(this, "The file was sent",
Toast.LENGTH_SHORT).show();
}
}
**in the manifest I put:**
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.write_file"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/my_path" />
</provider>
<activity
android:name="com.example.write_file.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
**and in the my_path.xml :**
<paths>
<files-path name="files" path="files/"/>
</paths>
package com.example.write_文件;
+导入java.io.FileOutputStream;
公共类MainActivity扩展活动{
私有编辑文本输入;
int D;
字符串W;
char-chr;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getInput=(EditText)findViewById(R.id.getInput);
D=0;
TextView textView1=(TextView)findViewById(R.id.textView1);
textView1.setText(“在txt文件中写入要发送的数字”);
}
public void onClick(视图){
写入_文件();
D=D+1;
W=getInput.getText().toString();//将获取一个字符串
如果(getInput.getText().length()>0){
chr=W.charAt(0);}//W转换为char
开关(view.getId()){
案例R.id.button1:
如果(((chr==(char)45)&&&(getInput.getText().length()<2))| |(getInput.getText().length()==0)){
Toast.makeText(这是“请输入文本”,
Toast.LENGTH_LONG).show();
返回;
}
TextView getInput1=(TextView)findViewById(R.id.textView1);
getInput1.setText(“在txt文件中写入要发送的数字”);
中断;}
}
公共void onClick2(视图){
sendfile();
完成();
}
public void write_file(){//写入本地而不是SD卡
请尝试下面的{//IOException
FileOutputStream fOut=openFileOutput(“samplefile.txt”,模式_-APPEND);
OutputStreamWriter osw=新的OutputStreamWriter(fOut);
osw.append(W+“\n”);//将字符串写入文件-“\n”,类似于pascal中的writeln(使用写字板而不是记事本)
osw.flush();
osw.close();
如果(D>=55){finish();}
Toast.makeText(这是“我正在向文件写入”,
吐司。长度(短)。show();
//您可以在DDMS文件资源管理器中找到的文件-“/data/data/your_project\u package\u structure/files/samplefile.txt”
//有一个图标手机用于从设备中提取文件或将文件放到设备上
}
捕获(ioe异常ioe)
{Toast.makeText(这个“异常文件不存在?”,
Toast.LENGTH_SHORT).show();}
}
/*
公共void sendfile(){
字符串文件;
nfile=“samplefile.txt”;
意向emailIntent=新意向(Intent.ACTION\u SEND);
emailIntent.setType(“文本/普通”);
emailIntent.putExtra(android.content.Intent.ACTION_SENDTO,新字符串[])
{"xxxx@yyyy.com"});
emailIntent.addFlags(Intent.FLAG\授予\读取\ URI\权限);
emailIntent.putExtra(Intent.EXTRA_主题,
“附件。”);
emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(“data/data/com.example.write_file/files”+nfile));
startActivity(Intent.createChooser(emailIntent,
“使用…发送电子邮件”);
Toast.makeText(这是“文件已发送”,
吐司。长度(短)。show();
} */
公共void sendfile(){
意向emailIntent=新意向(Intent.ACTION\u SEND);
emailIntent.setType(“文本/普通”);
emailIntent.putExtra(android.content.Intent.ACTION_SENDTO,新字符串[])
{"xxxx@yyyy.com"});
emailIntent.addFlags(Intent.FLAG\授予\读取\ URI\权限);
emailIntent.putExtra(Intent.EXTRA_主题,
“附件。”);
File File=getFileStreamPath(“samplefile.txt”);
emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(“文件:/”+file.getAbsolutePath());
startActivity(Intent.createChooser(emailIntent,
“使用…发送电子邮件”);
Toast.makeText(这是“文件已发送”,
吐司。长度(短)。show();
}
}
**我在舱单上写了:**
**在my_path.xml中:**
首先,第三方应用程序无法访问您的内部存储。将文件写入外部存储器或提供对内部存储器的选择性访问
其次,从不硬编码路径。您的代码将在许多Android设备上失败。使用
getFilesDir()
或getFileStreamPath()
,而不是硬编码data/data/com.example.write\u file/files
首先,第三方应用程序无法访问您的内部存储。将文件写入外部存储器或提供对内部存储器的选择性访问
其次,从不硬编码路径。您的代码将在许多Android设备上失败。使用
getFilesDir()
或getFileStreamPath()
,而不是硬编码data/data/com.example.write\u file/files
您必须使用外部存储器发送电子邮件。如果您使用当前正在使用的应用程序数据文件路径,则将获得“权限被拒绝”异常
而不是将文件保存在外部存储器中,然后使用Intent将其发送到。您必须使用外部存储器发送电子邮件。如果您使用当前正在使用的应用程序数据文件路径,则将获得“权限被拒绝”异常
相反,将文件保存在外部存储器中,然后使用Intent将其发送到。我编辑了代码(如上),但仍然无法工作。你能检查一下现在出了什么问题吗?thanks@user2556278:如我所写,第三方应用程序无法访问您的内部存储。将文件写入外部存储或尝试
FileProvider
提供对内部存储的选择性访问。@m0skit0:哦,这也很糟糕,但这不会阻止代码工作。我编辑了代码(上面),但它仍然不工作。你能