1

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

我做了一些研究,发现了这个问题,但没有答案......

我尝试过的更多哑剧类型:

application/x-compressed, application/x-zip-compressed
multipart/x-zip and application/octet-stream

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

谢谢。

4

2 回答 2

4

经过一番麻烦,我发现通过 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-05-09T13:29:43.413 回答
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();  
                  }  
    }  
  }  
于 2013-10-12T07:32:21.147 回答