1

Everyone: I am trying to send a folder (there are many files inside this folder) via email in Android Development.

First, I tried send the whole folder directly by using a click event and intent event.

My first attempt code shows the following:

My first part of code is onclicklistener event:

listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(int position, SwipeMenu menu,int index) {

                    switch (index) {
                        case 0:
                            sendEmail(list.get(position).getName());
                            break;

                        case 1:
                            list.remove(position);
                            adapter.notifyDataSetChanged();

                    }

                    return false;
                }
            });

My second code to send Email is as follows:

    public void sendEmail(String data_path){
        Intent email = new Intent(Intent.ACTION_SEND);
        File file_location = new File(SDCard, data_path);
        email.setType("vnd.android.cursor.dir/email");
        email.putExtra(Intent.EXTRA_EMAIL, new String[]{"example@gmail.com"});  //set up email
        email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file_location));   //add attachment
        email.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        startActivity(Intent.createChooser(email, "pick an Email provider"));

}

When I run this code, it works fine to jump into email sender, but without any folder implement, the email implement is empty.

I am wondering if it is impossible to send a folder directly via email.

Now I am trying to another way to solve this: I am planning to compress folder(.zip) first and then send the zip file to email in just one click event, But I can not find any solutions showing how to compress the folder and send zip file in just one click event, What I mean is that I want a solution which:

  1. Clicks the file that needs to be sent (click event has finished)
  2. After it triggers the click event, the app will compress the clicked file to a zip file.
  3. The zip file will automatically add as mail implements that waits to be sent

I was trapped there for many days and still failed to find any answers, I also search on StackOverflow, but most questions are about how to compress a file or send file by email. I am looking for a way to compress a folder and send a zip file in one click event.

If you have any ideas, I would quite appreciate them!

Mark Han
  • 2,785
  • 2
  • 16
  • 31
bricker
  • 55
  • 12
  • You cant send folders directly from email without compressing them into .zip or .rar first, also you need to send the folder or you can send the files independient ? if you can send the files without the folder you should loop trought your folder and send all the files to the email – Gastón Saillén Mar 10 '18 at 02:13
  • @Gastón Saillén, Thanks for your suggestion, I also considered your solution, the problem is there are more than 50+ files inside this folder, if i send each file independently, It will be a bunch of email implments that will affect the effecient, I just wantto send this whole folder (add this folder as implement) by email without send individual files one by one, But I tried send folder directly, it also failed (mail implements is empty). I was trapped there. – bricker Mar 10 '18 at 02:22
  • i understand, did you checked this ? https://stackoverflow.com/questions/25562262/how-to-compress-files-into-zip-folder-in-android – Gastón Saillén Mar 10 '18 at 02:24

1 Answers1

1

Here is a workaround to transform your folder into zip.

First, grant permissions:

<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

And then use this to transform your folder:

/*
 * 
 * Zips a file at a location and places the resulting zip file at the toLocation
 * Example: zipFileAtPath("downloads/myfolder", "downloads/myFolder.zip");
 */

public boolean zipFileAtPath(String sourcePath, String toLocation) {
    final int BUFFER = 2048;

    File sourceFile = new File(sourcePath);
    try {
        BufferedInputStream origin = null;
        FileOutputStream dest = new FileOutputStream(toLocation);
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                dest));
        if (sourceFile.isDirectory()) {
            zipSubFolder(out, sourceFile, sourceFile.getParent().length());
        } else {
            byte data[] = new byte[BUFFER];
            FileInputStream fi = new FileInputStream(sourcePath);
            origin = new BufferedInputStream(fi, BUFFER);
            ZipEntry entry = new ZipEntry(getLastPathComponent(sourcePath));
            out.putNextEntry(entry);
            int count;
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
        }
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

Here is another example:

private static void zipFolder(String inputFolderPath, String outZipPath) {
    try {
        FileOutputStream fos = new FileOutputStream(outZipPath);
        ZipOutputStream zos = new ZipOutputStream(fos);
        File srcFile = new File(inputFolderPath);
        File[] files = srcFile.listFiles();
        Log.d("", "Zip directory: " + srcFile.getName());
        for (int i = 0; i < files.length; i++) {
            Log.d("", "Adding file: " + files[i].getName());
            byte[] buffer = new byte[1024];
            FileInputStream fis = new FileInputStream(files[i]);
            zos.putNextEntry(new ZipEntry(files[i].getName()));
            int length;
            while ((length = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }
            zos.closeEntry();
            fis.close();
        }
        zos.close();
    } catch (IOException ioe) {
        Log.e("", ioe.getMessage());
    }
}

Also you can use this library to zip a folder or file.

Import the .jar into your project and then you can do this to transform what you need:

try {
    File input = new File("path/to/your/input/fileOrFolder");
    String destinationPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "zippedItem.zip";
    ZipParameters parameters = new ZipParameters();
    parameters.setCompressionMethod(Zip4jConstants.COMP_STORE);
    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
    File output = new File(destinationPath);
    ZipFile zipFile = new ZipFile(output);
    // .addFolder or .addFile depending on your input
    if (sourceFile.isDirectory())
        zipFile.addFolder(input, parameters);
    else
        zipFile.addFile(input, parameters);
    // Your input file/directory has been zipped at this point and you
    // can access it as a normal file using the following line of code
    File zippedFile = zipFile.getFile();
} catch (ZipException e) {
    Log.e(TAG, Log.getStackTraceString(e));
}

This should do the trick.

halfer
  • 19,824
  • 17
  • 99
  • 186
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77