【问题标题】:Android: Share zip file via GmailAndroid:通过 Gmail 共享 zip 文件
【发布时间】:2012-05-09 09:47:19
【问题描述】:

我正在开发一个 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

这个问题有解决办法吗??

谢谢。

【问题讨论】:

    标签: android gmail mime-types


    【解决方案1】:

    经过一番折腾,我发现通过 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

    【讨论】:

    • 这适用于发送 KMZ 文件。太棒了......但是(不幸的是)它提供了很多额外的应用程序,除了 gmail 应用程序之外,这些应用程序并不真正相关。
    • 哇哇哇,我找到了this other answer,我可以将EXTRA_STREAM 添加到GMail、Google Drive 和Skype 作为候选应用程序
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2012-06-11
      • 2019-11-25
      • 2012-05-09
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多