android - 如何发送带有 Intent 的 zip 文件?安卓

标签 android android-intent zip

我有下面的代码来压缩我想要的东西。

    public class JCreateZIP {

    private static final int BUFFER = 80000;

    public int zip(List<String> _files, String zipFileName) {
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(zipFileName);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                    dest));
            byte data[] = new byte[BUFFER];

            for (int i = 0; i < _files.size(); i++) {
                Log.v("Compress", "Adding: " + _files.get(i));
                FileInputStream fi = new FileInputStream(_files.get(i));
                origin = new BufferedInputStream(fi, BUFFER);

                ZipEntry entry = new ZipEntry(_files.get(i).substring(_files.get(i).lastIndexOf(File.separator) + 1));
                out.putNextEntry(entry);
                int count;

                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }

            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }

    private void dirChecker(String dir) {
        File f = new File(dir);
        if (!f.isDirectory()) {
            f.mkdirs();
        }
    }

public List<String> getListFiles(File file, List<String> files) {
    File[] dirs = file.listFiles();
    String name = "";
    if (dirs != null) {
        for (File dir : dirs) {
            if (dir.isFile()) {
                name = dir.getName().toLowerCase();
                if(name.endsWith(".png") || name.endsWith(".jpg")) {
                    files.add(dir.getAbsolutePath());
                }
            } else files = getListFiles(dir, files);
        }
    }
    return files;
}

}

此代码在单击按钮(共享)时执行。检查下面的代码。

    String folderPath = Environment.getExternalStorageDirectory() +          File.separator + "Folder" + File.separator;
            String zipFile = "Item.zip";
            File dir = new File(folderPath);
            List<String> listFiles;

            JCreateZIP zipManager = new JCreateZIP();

            listFiles = zipManager.getListFiles(dir, new ArrayList<String>());

            if(zipManager.zip(listFiles, folderPath + zipFile) == 1) {
                Toast.makeText(getApplicationContext(), "Item.zip created!", Toast.LENGTH_LONG).show();

                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(folderPath));
                sendIntent.setType("application/zip");
                startActivity(sendIntent);
            }

但是当我尝试通过电子邮件发送 folder.zip 时,蓝牙或 gdrive 不起作用。我能做什么?

最佳答案

我传递了错误的 Uri,我更改了这个:

    String folderPath = Environment.getExternalStorageDirectory() +          File.separator + "Folder" + File.separator;
        String zipFileName = "Item.zip";
        File dir = new File(folderPath);
        List<String> listFiles;
        File zipFile = File (folderPath + zipFileName);

        JCreateZIP zipManager = new JCreateZIP();

        listFiles = zipManager.getListFiles(dir, new ArrayList<String>());

        if(zipManager.zip(listFiles, folderPath + zipFileName) == 1) {
            Toast.makeText(getApplicationContext(), "Item.zip created!", Toast.LENGTH_LONG).show();

            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(zipFile));
            sendIntent.setType("application/zip");
            startActivity(sendIntent);
        }

关于android - 如何发送带有 Intent 的 zip 文件?安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32479585/

相关文章:

android - 由于使用 bundletool 的 java 堆空间,从 App Bundle 中提取 APK 失败

java - Android ByteBuffer 到 float 转换返回错误值

java - 致命异常 : main java. lang.NoClassDefFoundError : com. google.analytics.tracking.android.EasyTracker

android - 单击时 ListView

android - Intent 过滤器 : Using two actions along with Launcher and Browsable category

android - 如何不在主线程上运行服务?

Android选择 Activity 文字颜色

linux - 使用 make 压缩子文件夹中的特定文件

java - 下载的 zip 文件无效

java - 为什么没有 ZipInputStream 收缩?