Eduardo Bonfa
Eduardo Bonfa

Reputation: 288

how to send zip files with intent? android

I have the code bellow to compress what I want.

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

}

This code executes when a button (Share) is clicked. Check the code bellow.

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

But when I try to send the folder.zip by email, bluetooth or gdrive doesn't work. What can I do?

Upvotes: 3

Views: 6820

Answers (1)

Eduardo Bonfa
Eduardo Bonfa

Reputation: 288

I was passing the wrong Uri, I changed this:

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

Upvotes: 5

Related Questions