【问题标题】:How to compress a folder and send via email in android如何在android中压缩文件夹并通过电子邮件发送
【发布时间】:2018-03-10 02:11:50
【问题描述】:

大家:我正在尝试在 Android 开发中通过电子邮件发送一个文件夹(该文件夹中有很多文件)。

首先,我尝试使用点击事件和意图事件直接发送整个文件夹。

我的第一次尝试代码显示如下:

我的第一部分代码是 onclicklistener 事件:

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

我发送电子邮件的第二个代码如下:

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

}

当我运行这段代码时,它可以很好地跳转到电子邮件发件人,但是没有任何文件夹工具,电子邮件工具是空的。

我想知道是否不能直接通过电子邮件发送文件夹。

现在我正在尝试另一种方法来解决这个问题:我打算先压缩文件夹(.zip),然后通过单击事件将 zip 文件发送到电子邮件,但我不能找到任何显示如何压缩文件夹并在单击事件中发送 zip 文件的解决方案,我的意思是我想要一个解决方案:

  1. 点击需要发送的文件(点击事件已完成)
  2. 触发点击事件后,应用会将点击的文件压缩成zip文件。
  3. zip 文件将自动添加为等待发送的邮件工具

我被困在那里很多天仍然没有找到任何答案,我也在 StackOverflow 上搜索,但大多数问题是关于如何压缩文件或通过电子邮件发送文件。我正在寻找一种压缩文件夹并在单击事件中发送 zip 文件的方法。

如果您有任何想法,我将不胜感激!

【问题讨论】:

  • 如果不先将文件夹压缩成 .zip 或 .rar 就不能直接从电子邮件发送文件夹,还需要发送文件夹还是可以独立发送文件?如果您可以在没有文件夹的情况下发送文件,您应该循环访问您的文件夹并将所有文件发送到电子邮件
  • @Gastón Saillén,谢谢你的建议,我也考虑了你的解决方案,问题是这个文件夹里有超过 50 多个文件,如果我单独发送每个文件,那将是一堆会影响效率的电子邮件工具,我只想通过电子邮件发送整个文件夹(添加此文件夹作为工具),而不是逐个发送单个文件,但是我尝试直接发送文件夹,它也失败了(邮件工具为空)。我被困在那里。
  • 我明白了,你检查过这个吗? stackoverflow.com/questions/25562262/…

标签: java android android-studio


【解决方案1】:

这是将文件夹转换为 zip 的解决方法。

首先,授予权限:

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

然后用它来转换你的文件夹:

/*
 * 
 * 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;
    }

这是另一个例子:

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

您也可以使用this library 压缩文件夹或文件。

将 .jar 导入到您的项目中,然后您可以执行此操作来转换您需要的内容:

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

这应该可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    相关资源
    最近更新 更多