Android-无法在电子邮件中附加文件
默认情况下,保存到内部存储器的文件是应用程序的专用文件,其他应用程序无法访问它们(用户也不能) 我能够在DDMS中看到文件“/data/data/package_name/files/in file explore,但是当我在电子邮件中使用imageUri附加上述文件URI时,我看到附加的文件是0kb。 我使用了Android的默认电子邮件API 谁能建议我,如何在电子邮件中附加一个应用程序专用的文件 虽然我能够成功地将文件保存在SD卡中并从SD卡附加文件,但工作正常 但是,如果SD卡不可用,并将文件保存到内部存储器中,那么如何在电子邮件中附加它们Android-无法在电子邮件中附加文件,android,email,attachment,Android,Email,Attachment,默认情况下,保存到内部存储器的文件是应用程序的专用文件,其他应用程序无法访问它们(用户也不能) 我能够在DDMS中看到文件“/data/data/package_name/files/in file explore,但是当我在电子邮件中使用imageUri附加上述文件URI时,我看到附加的文件是0kb。 我使用了Android的默认电子邮件API 谁能建议我,如何在电子邮件中附加一个应用程序专用的文件 虽然我能够成功地将文件保存在SD卡中并从SD卡附加文件,但工作正常 但是,如果SD卡不可用,并
String FILENAME = "hello_file.txt";
String string = "hello world!";FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
File imageFile = getFileStreamPath(FILENAME );
Uri imageUri = Uri.fromFile(imageFile);
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("*/*");
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,imageUri);
this.startActivityForResult(Intent.createChooser(emailIntent, "Send mail..."),SUB_ACTIVITY);
保存文件时,请尝试使用而不是Context.MODE\u PRIVATE。然后其他应用程序将可以访问该文件。当您尝试从内部存储附加文件时,GMail会将错误写入日志:
ERROR/Gmail(...): file:// attachment paths must point to file:///mnt/sdcard.
电子邮件应用程序将向您显示附加的文件,即使它实际上并不存在
对于外部存储器,文档说明:
每个Android兼容设备都支持一个共享的“外部存储”,您可以使用它来保存文件。它可以是可移动存储介质(如SD卡)或内部(不可移动)存储
这意味着您不必担心设备根本没有外部存储。尽管如此,外部存储有时可能不可用。参考,我在使用内部文件时也遇到过这个问题,尽管我在/data/data//files/testFileName.txt上使用了openFileInput和MODE_WORLD_READABLE,并在/data/data//files/testFileName.txt上使用了URI.parse和the extra“/”(见下文),收到的测试电子邮件仍然缺少所需的附件。很抱歉,除了尝试使用SD卡上的外部文件外,没有其他答案-这是我的下一个实验 代码:
为了共享私有文件,您需要使用ContentProvider来提供其他应用程序对您的文件的访问。下面是一个很好的示例:
此外,尽管本教程提到您需要在Android清单文件中声明您的提供者,但它没有指定它应该包含在
中,因此请确保当您声明它在
中时,此代码可能会帮助您了解附件:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSend = (Button) findViewById(R.id.buttonSend);
textTo = (EditText) findViewById(R.id.editTextTo);
textSubject = (EditText) findViewById(R.id.editTextSubject);
textMessage = (EditText) findViewById(R.id.editTextMessage);
buttonSend.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
String to = textTo.getText().toString();
String subject = textSubject.getText().toString();
String message = textMessage.getText().toString();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("plain/text");
File data = null;
try {
Date dateVal = new Date();
String filename = dateVal.toString();
data = File.createTempFile("Report", ".csv");
FileWriter out = (FileWriter) GenerateCsv.generateCsvFile(
data, "Name,Data1");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(data));
i.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(i, "E-mail"));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public class GenerateCsv
{
public static FileWriter generateCsvFile(File sFileName,String fileContent)
{
FileWriter writer = null;
try {
writer = new FileWriter(sFileName);
writer.append(fileContent);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return writer;
}
}
上述代码要求您向清单文件添加以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Android:将文件从内部缓存附加到Gmail
包com.stephendnicholas.gmailattach;
导入java.io.File;
导入java.io.FileNotFoundException;
导入android.content.ContentProvider;
导入android.content.ContentValues;
导入android.content.UriMatcher;
导入android.database.Cursor;
导入android.net.Uri;
导入android.os.ParcelFileDescriptor;
导入android.util.Log;
公共类CachedFileProvider扩展了ContentProvider{
私有静态最终字符串CLASS\u NAME=“CachedFileProvider”;
//authority是提供程序类的符号名
public static final String AUTHORITY=“com.stephendnicholas.gmailattach.provider”;
//用于匹配传入请求的UriMatcher
私人UriMatcher UriMatcher;
@凌驾
公共布尔onCreate(){
uriMatcher=新的uriMatcher(uriMatcher.NO_匹配);
//向匹配器添加一个URI,该URI将与表单匹配
// 'content://com.stephendnicholas.gmailattach.provider/*'
//如果传入Uri与此模式匹配,则返回1
addURI(AUTHORITY,“*”,1);
返回true;
}
@凌驾
公共ParcelFileDescriptor openFile(Uri,字符串模式)
抛出FileNotFoundException{
字符串LOG\u TAG=CLASS\u NAME+“-openFile”;
Log.v(Log_标签,
使用uri调用:“+uri+”。“+uri.getLastPathSegment());
//根据匹配器检查传入Uri
开关(uriMatcher.match(uri)){
//如果返回1-则与onCreate中定义的Uri匹配
案例1:
//所需的文件名由文件的最后一段指定
//路径
//例如。
// 'content://com.stephendnicholas.gmailattach.provider/Test.txt'
//获取该文件并构建该文件的路径
字符串fileLocation=getContext().getCacheDir()+File.separator
+getLastPathSegment();
//创建并返回指向文件的ParcelFileDescriptor(&R)
//注:我不在乎他们要求什么模式-他们只会得到
//只读
ParcelFileDescriptor pfd=ParcelFileDescriptor.open(新文件(
文件位置),ParcelFileDescriptor.MODE\u只读);
返回pfd;
//否则无法识别的Uri
违约:
Log.v(Log_标记,“不受支持的uri:”“+uri+””);
抛出新的FileNotFoundException(“不支持的uri:”
+uri.toString());
}
}
// //////////////////////////////////////////////////////////////
//此示例不支持/使用/需要
// //////////////////////////////////////////////////////////////
@凌驾
公共int更新(Uri、ContentValues、ContentValues、字符串s、,
字符串[]作为{
返回0;
}
@凌驾
public int delete(Uri,字符串s,字符串[]as){
返回0;
}
@凌驾
公共Uri插入(Uri Uri,ContentValues ContentValues){
返回null;
}
@凌驾
公共字符串getType(Uri){
返回null;
}
@凌驾
公共游标查询(Uri、字符串[]投影、字符串s、字符串[]as1、,
字符串s1){
返回null;
}
}
我也面临着同样的问题,而following为我工作
首先发送广播到not
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Android: Attaching files from internal cache to Gmail
package com.stephendnicholas.gmailattach;
import java.io.File;
import java.io.FileNotFoundException;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.util.Log;
public class CachedFileProvider extends ContentProvider {
private static final String CLASS_NAME = "CachedFileProvider";
// The authority is the symbolic name for the provider class
public static final String AUTHORITY = "com.stephendnicholas.gmailattach.provider";
// UriMatcher used to match against incoming requests
private UriMatcher uriMatcher;
@Override
public boolean onCreate() {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
// Add a URI to the matcher which will match against the form
// 'content://com.stephendnicholas.gmailattach.provider/*'
// and return 1 in the case that the incoming Uri matches this pattern
uriMatcher.addURI(AUTHORITY, "*", 1);
return true;
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
String LOG_TAG = CLASS_NAME + " - openFile";
Log.v(LOG_TAG,
"Called with uri: '" + uri + "'." + uri.getLastPathSegment());
// Check incoming Uri against the matcher
switch (uriMatcher.match(uri)) {
// If it returns 1 - then it matches the Uri defined in onCreate
case 1:
// The desired file name is specified by the last segment of the
// path
// E.g.
// 'content://com.stephendnicholas.gmailattach.provider/Test.txt'
// Take this and build the path to the file
String fileLocation = getContext().getCacheDir() + File.separator
+ uri.getLastPathSegment();
// Create & return a ParcelFileDescriptor pointing to the file
// Note: I don't care what mode they ask for - they're only getting
// read only
ParcelFileDescriptor pfd = ParcelFileDescriptor.open(new File(
fileLocation), ParcelFileDescriptor.MODE_READ_ONLY);
return pfd;
// Otherwise unrecognised Uri
default:
Log.v(LOG_TAG, "Unsupported uri: '" + uri + "'.");
throw new FileNotFoundException("Unsupported uri: "
+ uri.toString());
}
}
// //////////////////////////////////////////////////////////////
// Not supported / used / required for this example
// //////////////////////////////////////////////////////////////
@Override
public int update(Uri uri, ContentValues contentvalues, String s,
String[] as) {
return 0;
}
@Override
public int delete(Uri uri, String s, String[] as) {
return 0;
}
@Override
public Uri insert(Uri uri, ContentValues contentvalues) {
return null;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Cursor query(Uri uri, String[] projection, String s, String[] as1,
String s1) {
return null;
}
}
<provider android:name="CachedFileProvider" android:authorities="com.stephendnicholas
public static void createCachedFile(Context context, String fileName,
String content) throws IOException {
File cacheFile = new File(context.getCacheDir() + File.separator
+ fileName);
cacheFile.createNewFile();
FileOutputStream fos = new FileOutputStream(cacheFile);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
PrintWriter pw = new PrintWriter(osw);
pw.println(content);
pw.flush();
pw.close();
}
public static Intent getSendEmailIntent(Context context, String email,
String subject, String body, String fileName) {
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
//Explicitly only use Gmail to send
emailIntent.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
emailIntent.setType("plain/text");
//Add the recipients
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { email });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
//Add the attachment by specifying a reference to our custom ContentProvider
//and the specific file of interest
emailIntent.putExtra(
Intent.EXTRA_STREAM,
Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/"
+ fileName));
return emailIntent;
}
enter code here
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+storagePath)));
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, "Receiver Email Address" );
email.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
email.putExtra(Intent.EXTRA_SUBJECT, "Subject");
email.putExtra(Intent.EXTRA_TEXT,"Email Text");
//Mime type of the attachment (or) u can use sendIntent.setType("*/*")
//email.setType("text/plain");
email.setType("application/YourMimeType");
//Full Path to the attachment
email.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+storagePath));
try
{
startActivity(Intent.createChooser(email, "Send Message..."));
}
catch (android.content.ActivityNotFoundException ex)
{
}