我正在开发一个Android应用程序,它应该允许用户通过Gmail分享他们的内容。我使用的是android版本2.2(Froyo)。问题是我找不到任何有效的解决方案,我几乎尝试了所有的方法,但都没有成功。这是我使用的代码:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);;
sharingIntent.setType("application/zip");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
getString(R.string.share_subject));
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, getString(R.string.share_body));
String zipFile = FileProvider.URI_AUTHORITY + File.separator + mItemSelected.getLibraryName() + File.separator + mItemSelected.getZipFileName();
sharingIntent.putExtra(Intent.EXTRA_STREAM, android.net.Uri.parse(zipFile));
startActivity(Intent.createChooser(sharingIntent, (getString(R.string.share_chooser))));
}这种情况下的问题是,Gmail应用程序无缘无故地替换了文件的mime类型,并将文件显示为text/html,然后我的应用程序就不会显示在可以处理这种文件的应用程序列表中。另一个限制是我不想在我的意图过滤器中使用text/html,因为我希望它尽可能地集中,如果可能的话,我会定义我自己的mime类型……
我做了一点研究,找到了这个question,但没有答案...
我尝试了更多的mime类型:
application/x-compressed, application/x-zip-compressed
multipart/x-zip and application/octet-stream这个问题有什么解决办法吗??
谢谢。
发布于 2013-05-09 21:29:43
在经历了许多麻烦之后,我发现通过Intent启动的Gmail并不喜欢以.zip为前缀的附件。因此,在将其重命名为".vip“后,我成功地发送了附件。下面是一段代码(outFile是一个重命名为“.vip”的压缩文件):
enter
private void sendMail(File outFile) {
Uri uriToZip = Uri.fromFile(outFile);
String sendText = "Dear friend,\n\n...";
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { "checcodotti@gmail.com" });
sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, sendText);
sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Log of the test " + expFilename);
// sendIntent.setType("image/jpeg");
// sendIntent.setType("message/rfc822");
sendIntent.setType("*/*");
sendIntent.putExtra(android.content.Intent.EXTRA_STREAM, uriToZip);
startActivity(Intent.createChooser(sendIntent, "Send Attachment !:"));
}如果有帮助,请告诉我。关于FD
发布于 2013-10-12 15:32:21
我改进了我之前关于“压缩”部分的回答。现在,通过GMail或其他方式发送的.zip附件没有问题。试试这个:
{
int lung;
FileInputStream in;
FileOutputStream out;
byte[] buffer = new byte[DIM_BUFFER];
// compress the file to send
String inPath = ctx.getApplicationContext().getFilesDir().getAbsolutePath();
outFile = new File(outPath,TestEdit.ZIPNAME);
// outFile = new File(outPath,filename + ".vip");
in = new FileInputStream(inFile);
ZipEntry entry = new ZipEntry(filename + ".csv");
try{
out = new FileOutputStream(outFile);
// GZIPOutputStream zos;
ZipOutputStream zos;
zos = new ZipOutputStream(new BufferedOutputStream(out) );
zos.putNextEntry(entry);
try {
while ((lung=in.read(buffer)) > 0) {
Log.v(TAG, "Lunghezza di in=" + lung + ". Lungh di buffer=" + buffer.length );
if (buffer.length == lung) {
zos.write(buffer);
} else {
// Gestione del caso in cui il buffer non sia pieno
for (int b = 0; b < lung; b++) {
zos.write(buffer[b]);
}
}
}
} finally {
zos.closeEntry();
zos.close();
in.close();
out.close();
}
}
} https://stackoverflow.com/questions/10371065
复制相似问题