本文介绍了如何在Android中使用ICal4j解析ICS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解析的.ics日历文件中看到的 http://ical4j.sourceforge.net /introduction.html 。如何使用我的project.I的ical4j-1.0-rc3.jar文件需要帮助与此相关的PLZ?

parse .ics Calendar file using Ical4J in android.i saw the http://ical4j.sourceforge.net/introduction.html. How to use that ical4j-1.0-rc3.jar file in my project.I need help related to this plz ?

推荐答案

嘿,为下面项目中的jar文件只需配置清单:

  1. ical4j-1.0.3.jar
  2. 共享记录-1.1.1.jar
  3. 在公郎-2.6.jar
  4. 的commons-IO-2.1.jar
  5. commons- codeC-1.5.jar
  6. 的backport-util的并发-3.1.jar

希望这是帮助你..!

下面是示例$ C $下您:

   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:[email protected]"));
        dev1.getParameters().add(Role.REQ_PARTICIPANT);
        dev1.getParameters().add(new Cn("For Ashok"));
        christmas.getProperties().add(dev1);

        Attendee dev2 = new Attendee(URI.create("mailto:[email protected]"));
        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;
        }
    }


}

这篇关于如何在Android中使用ICal4j解析ICS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:13