如何发送有意图的zip文件?机器人

时间:2015-09-09 12:29:06

标签: 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无法正常工作。我能做什么?

1 个答案:

答案 0 :(得分:3)

我传递了错误的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);
        }