2012-04-29 81 views
1

我正在开发一款Android应用程序,该应用程序允许用户通过Gmail共享其内容。我使用的是Android 2.2版(Froyo)。 问题是我找不到任何工作解决方案,我尝试了几乎所有的东西,但没有运气。 这是我使用的代码:Android:通过Gmail共享zip文件

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 

是否有此问题的任何解决方案?

谢谢。

回答

3

经过很多麻烦,我发现通过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[] { "[email protected]" }); 
     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 !:")); 
    } 

请让我知道,如果它帮助。 Registers FD

+1

这适用于发送KMZ文件。太棒了......但(不幸的是)它提供了很多额外的应用程序,这些应用程序在gmail应用程序旁边并不相关。 – nyaray 2013-08-28 21:11:19

+0

Woop woop,我发现[这个其他答案](http://stackoverflow.com/questions/2197741/how-to-send-email-from-my-android-application),我可以添加'EXTRA_STREAM'到并把它归结为GMail,Google Drive和Skype作为候选应用程序 – nyaray 2013-08-28 21:29:43

0

我改进了我之前对“压缩”部分的回答。现在,通过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(); 
        } 
    } 
    }