Android: Share zip file via Gmail

99封情书 提交于 2019-12-08 05:09:32

问题


I'm working on an Android application that should allow the users to share their content via Gmail. I'm using android version 2.2(Froyo). The problem is that I can't find any working solution for this, I tried almost everything ,but with no luck. this is the code I'm using:

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))));
}

The problem in this case is that the Gmail app, for no obvious reason, is replacing the mime type of the file, and show the file as text/html, and then my application is not shown in the application list that can deal with this kind of file. Another restriction is that I don't want to use text/html in my intent filter, because I want it to be focused as much as possible, and if it were possible I would define my own mime type...

I did a little research and found this question, but with no answers...

More mime types I tried:

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

Is there any solution for this problem ??

Thanks.


回答1:


after a lot of trouble, I discovered that Gmail, launched via Intent, does not like attachments whose prefix is .zip. So, I succeeded in sending the attachments after renaming it ".vip". Here is a piece of code (outFile is a zipped file renamed as ".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 !:"));
    }

Please let me know if it helps. Regards FD




回答2:


I improve my previous answer for the part concerning "zipping". Now the are no problems with .zip attachments sent via GMail or whatever. Try this:

 {  
              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/android-share-zip-file-via-gmail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!