快速发送Android中的大文件

时间:2014-07-16 09:57:11

标签: java android android-asynctask

我想发送18 MB的数据。这是工作。但我必须等待太长时间才能收到电子邮件。

代码:

public void sendEmail()
{
    emailSendReceiver = new EmailSendBroadcastReceiver();
    EmailSend emailSend = new EmailSend();
    emailSend.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

public class EmailSend extends AsyncTask<Void, Void, Boolean>
{
    @Override
    protected Boolean doInBackground(Void... params)
    {
        boolean bResult = false;
        String sDeviceID = configReader.getXmlValue(KEY_ID);
        Mail m = new Mail("test@gmail.com", "testpass");
        String[] toArr = {"toEmail@gmail.com"};
        m.setTo(toArr); 
        m.setFrom("noreply@something.com"); 
        m.setSubject("device number : "+sDeviceID ); 
        m.setBody("device number : "+sDeviceID);
        try
        {
            String sTxtFileName = sDeviceID+"_"+".txt";
            String sFileUrl = Environment.getExternalStorageDirectory().getAbsolutePath()+"/data_source/"+sTxtFileName;
            m.addAttachment(sFileUrl);
            if(m.send())
            {
                bResult = true;
            }
            else
            {
                // something
            }
        }
        @Override
        protected void onPostExecute(Boolean result)
        {
            super.onPostExecute(result);
            if(result == true)
            {
                // something
            }
        }
    }
}

问题是。我怎样才能让它更快?我有6个AsyncTask。而且我不想用活动来制作它。

2 个答案:

答案 0 :(得分:1)

正如所有人所建议的那样压缩或压缩文件会很方便。在

中也有相同的内容 java.util.zip* 包。此外,您可以找到相同here

的帮助

答案 1 :(得分:0)

public void makeZip(String sFile, String zipFileName)
{
    FileOutputStream dest = null;
    ZipOutputStream out;
    byte data[];
    FileInputStream fi = null;
    int count;

    try
    {
        dest = new FileOutputStream(zipFileName);

        out = new ZipOutputStream(new BufferedOutputStream(dest));
        data = new byte[BUFFER];

        fi = new FileInputStream(sFile);

        BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry entry = new ZipEntry(sFile);

        out.putNextEntry(entry);

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

        origin.close();

        out.close();
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}