gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:33:33 32 4
gpt4 key购买 nike

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

最佳答案

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

  1. ical4j-1.0.3.jar
  2. commons-logging-1.1.1.jar
  3. commons-lang-2.6.jar
  4. commons-io-2.1.jar
  5. commons-codec-1.5.jar
  6. backport-util-concurrent-3.1.jar

希望这对你有帮助..!

这是为您准备的示例代码:

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


}

关于android - 如何在android中使用ICal4j解析ics文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2981339/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com