如何在android中使用ICal4j解析ics文件

时间:2010-06-05 17:30:40

标签: android

解析.ics在android.i中使用Ical4J的日历文件看到了http://ical4j.sourceforge.net/introduction.html。如何在我的项目中使用该ical4j-1.0-rc3.jar文件。我需要与此plz相关的帮助吗?

1 个答案:

答案 0 :(得分:7)

嘿,只需在项目中配置jar文件列表,如下所示:

  1. ical4j-1.0.3.jar
  2. 共享记录-1.1.1.jar
  3. 公地琅2.6.jar
  4. 公地-IO-2.1.jar
  5. 公地编解码器-1.5.jar
  6. 后向移植-util的并发-3.1.jar
  7. 希望对你有帮助..!

    以下是您的示例代码:

       package com.example.caltest;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.SocketException;
    import java.net.URI;
    import java.util.GregorianCalendar;
    
    import net.fortuna.ical4j.data.CalendarBuilder;
    import net.fortuna.ical4j.data.CalendarOutputter;
    import net.fortuna.ical4j.data.ParserException;
    import net.fortuna.ical4j.model.Calendar;
    import net.fortuna.ical4j.model.Component;
    import net.fortuna.ical4j.model.Date;
    import net.fortuna.ical4j.model.ParameterList;
    import net.fortuna.ical4j.model.Property;
    import net.fortuna.ical4j.model.PropertyList;
    import net.fortuna.ical4j.model.TimeZone;
    import net.fortuna.ical4j.model.TimeZoneRegistry;
    import net.fortuna.ical4j.model.TimeZoneRegistryFactory;
    import net.fortuna.ical4j.model.ValidationException;
    import net.fortuna.ical4j.model.component.VEvent;
    import net.fortuna.ical4j.model.component.VTimeZone;
    import net.fortuna.ical4j.model.parameter.Cn;
    import net.fortuna.ical4j.model.parameter.Encoding;
    import net.fortuna.ical4j.model.parameter.Role;
    import net.fortuna.ical4j.model.parameter.Value;
    import net.fortuna.ical4j.model.property.Attach;
    import net.fortuna.ical4j.model.property.Attendee;
    import net.fortuna.ical4j.model.property.CalScale;
    import net.fortuna.ical4j.model.property.ProdId;
    import net.fortuna.ical4j.model.property.Version;
    import net.fortuna.ical4j.util.UidGenerator;
    
    import org.apache.commons.io.output.ByteArrayOutputStream;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    
    public class MainActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            File file = new File(Environment.getExternalStorageDirectory()+"/1.ics");
            FileInputStream fin = null;
            try {
                fin = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            CalendarBuilder builder = new CalendarBuilder();
            Calendar calendar = null;
            try {
                calendar = builder.build(fin);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (calendar != null) {
                System.out.println("value :"+calendar);
                PropertyList plist = calendar.getProperties();
    
                for (Object object : plist.toArray()) {
                    System.out.println("oj :"+object);
                }
            }
    
            // Generate Event of Christmas Day
            Calendar mycalendar = new Calendar();
            mycalendar.getProperties().add(new ProdId("-//James Bond//iCal4j 1.0//EN"));
            mycalendar.getProperties().add(Version.VERSION_2_0);
            mycalendar.getProperties().add(CalScale.GREGORIAN);
    
            TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
            TimeZone timezone = registry.getTimeZone("Asia/Calcutta");
            VTimeZone tz = timezone.getVTimeZone();
    
            java.util.Calendar startEvent = new GregorianCalendar();
            startEvent.setTimeZone(timezone);
            startEvent.set(java.util.Calendar.MONTH, java.util.Calendar.JULY);
            startEvent.set(java.util.Calendar.DAY_OF_MONTH, 21);
            startEvent.set(java.util.Calendar.YEAR, 2012);
            startEvent.set(java.util.Calendar.HOUR, 9);
            startEvent.set(java.util.Calendar.MINUTE, 15);
            startEvent.set(java.util.Calendar.SECOND, 12);
    
            java.util.Calendar entEvent = new GregorianCalendar();
            entEvent.setTimeZone(timezone);
            entEvent.set(java.util.Calendar.YEAR, 2012);
            entEvent.set(java.util.Calendar.MONTH, java.util.Calendar.JULY);
            entEvent.set(java.util.Calendar.DAY_OF_MONTH, 21);
            entEvent.set(java.util.Calendar.HOUR, 16);
            entEvent.set(java.util.Calendar.MINUTE, 10);
            entEvent.set(java.util.Calendar.SECOND, 10);
    
    
            // initialise as an all-day event..
            VEvent christmas = new VEvent(new Date(startEvent.getTime()),new Date(entEvent.getTime()), "Shravan Mahino");
            christmas.getProperties().add(tz.getTimeZoneId());
    
            // Generate a UID for the event..
            UidGenerator ug = null;
            try {
                ug = new UidGenerator("2");
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
            christmas.getProperties().add(ug.generateUid());
            mycalendar.getComponents().add(christmas);
    
    
            Attendee dev1 = new Attendee(URI.create("mailto:ashok@vervesys.local"));
            dev1.getParameters().add(Role.REQ_PARTICIPANT);
            dev1.getParameters().add(new Cn("For Ashok"));
            christmas.getProperties().add(dev1);
    
            Attendee dev2 = new Attendee(URI.create("mailto:jigarp@vervesys.com"));
            dev2.getParameters().add(Role.OPT_PARTICIPANT);
            dev2.getParameters().add(new Cn("For Jigar P"));
            christmas.getProperties().add(dev2);
    
    
            FileInputStream fileattach = null;
            try {
                fileattach = new FileInputStream(Environment.getExternalStorageDirectory()+"ic_launcher_home.png");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            try {
                for (int i = fileattach.read(); i >= 0;) {
                    bout.write(i);
                    i = fileattach.read();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            ParameterList params = new ParameterList();
            params.add(Value.BINARY);
            params.add(Encoding.BASE64);
    
            Attach attach = new Attach(params, bout.toByteArray());
    
            VEvent eout = (VEvent) calendar.getComponent(Component.VEVENT);
    
            Attach aout = (Attach) eout.getProperty(Property.ATTACH);
    
    
            System.out.println("Result : "+GenerateICAL("Ashok.ics", mycalendar));
    
    
        }
    
        public Boolean GenerateICAL(String fileNameWithExtenion,Calendar calendar){
            FileOutputStream fout = null;
            try {
                fout = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+fileNameWithExtenion);
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            CalendarOutputter outputter;
            try {
                outputter = new CalendarOutputter();
                outputter.output(calendar, fout);
                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            } catch (ValidationException e) {
                e.printStackTrace();
                return false;
            }
        }
    
    
    }