gpt4 book ai didi

java - 如何压缩文件夹并在android中通过电子邮件发送

转载 作者:行者123 更新时间:2023-11-30 00:04:54 24 4
gpt4 key购买 nike

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

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

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

我的第一部分代码是 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;
}
});

我的第二个发送Email的代码如下:

    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 的解决方法。

首先,授予权限:

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

这应该可以解决问题。

关于java - 如何压缩文件夹并在android中通过电子邮件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49204661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com