gpt4 book ai didi

android - Ics文件未按预期在Android中解析和检索数据。 (使用ical4库)

转载 作者:行者123 更新时间:2023-11-29 18:26:33 25 4
gpt4 key购买 nike

我正在尝试使用示例代码库解析ics文件:
这是指向我正在使用的示例项目的链接/指的是:project link

一旦我尝试解析数据,它要么由于空指针异常而崩溃,要么抛出错误,指出无法解析文件。 (我在上面的链接中添加了该项目的整个代码,这是一个很小的项目)。
这是我为此使用的mainactivity代码:

public class MainActivity extends AppCompatActivity {

public static final String TAG = MainActivity.class.getCanonicalName();

public static final Boolean DEBUG = true; //BuildConfig.DEBUG;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = getIntent();
Log.d(TAG, "Dumping Intent start");
Log.d(TAG, "Action: "+i.getAction());
Log.d(TAG, "URI: "+i.getDataString());
Log.d(TAG, "Type: "+i.getType());
Bundle bundle = i.getExtras();
if (bundle != null) {
Set<String> keys = bundle.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
Log.d(TAG, "[" + key + "=" + bundle.get(key) + "]");
}
}
Log.d(TAG, "Dumping Intent end");

Log.d(TAG, getFilePathFromUri(this, i.getData()));
try {
startActivity(new ICALParser(new FileInputStream(getFilePathFromUri(this, i.getData()))).buildIntent());
} catch (Exception e) {
if(DEBUG) Log.e(TAG, "Couldn't parse",e);
}

if(DEBUG) Log.d(TAG, "Done");
finish();

}

//http://stackoverflow.com/a/23750660
public static String getFilePathFromUri(Context c, Uri uri) {
String filePath = new File(uri.getPath()).getAbsolutePath();
if ("content".equals(uri.getScheme())) {
String[] filePathColumn = { MediaStore.MediaColumns.DATA };
ContentResolver contentResolver = c.getContentResolver();
Cursor cursor = contentResolver.query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
} else if ("file".equals(uri.getScheme())) {
filePath = new File(uri.getPath()).getAbsolutePath();
}
return filePath;
}
}


问题似乎发生在:getFilePathFromUri中,它要么由于空指针异常而崩溃,例如:android.net.Uri.getScheme(),要么抛出随机解析错误,其内容每次都不相同。
这是我正在使用的解析器类:

public class ICALParser {
public static final String TAG = ICALParser.class.getCanonicalName();
public static final boolean DEBUG = MainActivity.DEBUG;

private Calendar calendar;
private VEvent event;

public ICALParser(InputStream ics) throws IOException, ParserException {
CalendarBuilder builder = new CalendarBuilder();
calendar = builder.build(ics);
event = findFirstEvent();
}

public Intent buildIntent(){
Intent i = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);

if(DEBUG) {
for (Object o : event.getProperties()) {
Property property = (Property) o;
Log.d(TAG, "Property [" + property.getName() + ", " + property.getValue() + "]");
}
}

i.putExtra(CalendarContract.Events.TITLE, getValueOrNull(Property.SUMMARY));
i.putExtra(CalendarContract.Events.DESCRIPTION, getValueOrNull(Property.DESCRIPTION));
i.putExtra(CalendarContract.Events.EVENT_LOCATION, getValueOrNull(Property.LOCATION));

long start = event.getStartDate().getDate().getTime();
long end = event.getEndDate().getDate().getTime();
i.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,start);
i.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end);

boolean allDayEvent = ((event.getProperty("X-MICROSOFT-CDO-ALLDAYEVENT") != null //Microsoft's custom field exists
&& event.getProperty("X-MICROSOFT-CDO-ALLDAYEVENT").getValue().equals("true")) // and is true, or
|| (end-start % 1000*60*60*24 == 0 ) ); //the duration is an integer number of days
i.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, allDayEvent);

i.putExtra(CalendarContract.Events.RRULE, getValueOrNull(Property.RRULE));

return i;
}

private String getValueOrNull(String name){
Property p = event.getProperty(name);
return p == null ? null : p.getValue();
}

private VEvent findFirstEvent(){
for (Object o : calendar.getComponents()) {
Component c = (Component) o;
VEvent e = c instanceof VEvent ? ((VEvent) c) : null;
if (e != null) {
return e;
}
}
return null;
}


}


上面附加的项目可以编译并且可以运行,但是,原始文件夹中附加的ics文件不会被解析并映射到我设备上的日历上。您知道如何解决此问题吗?

最佳答案

这似乎有些过大。为什么不通过以下代码将事件直接添加到您的日历中? :

Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setData(CalendarContract.Events.CONTENT_URI);
// pass the values below somehow , either through a callback or some method, whatever you're using.
intent.putExtra(CalendarContract.Events.TITLE, contentTitle);
intent.putExtra(CalendarContract.Events.EVENT_LOCATION, locationAddress);
intent.putExtra(CalendarContract.Events.DESCRIPTION, contentDescription);

intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTimeInMilliseconds);
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTimeInMilliseconds );
intent.putExtra(CalendarContract.Events.EVENT_TIMEZONE, eventTimeZone);
intent.putExtra(CalendarContract.Events.EVENT_END_TIMEZONE, eventTimeZone); // pass your event time zone here, if any.

// Making it private and shown as busy
intent.putExtra(CalendarContract.Events.ACCESS_LEVEL, CalendarContract.Events.ACCESS_PRIVATE);
intent.putExtra(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_BUSY);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

关于android - Ics文件未按预期在Android中解析和检索数据。 (使用ical4库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58632013/

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