问题
I am trying to parse ics file using a sample code library : Here's a link to the sample project I am using /referring to: project link
As soon as I try to parse the data, it either crashes with a null pointer exception or throws an error saying file cannot be parsed. ( I have added the entire code for the project in my link above, pretty small project). Here's the mainactivity code I am using for that:
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;
}
}
Issue seems to happen in : getFilePathFromUri where it either crashes with a null pointer exception as : android.net.Uri.getScheme() or throws a random parsing error which differs everytime in its content. Here's the parser class I am using :
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;
}
}
The project attached above does compile and is runnable, however, ics files attached in the raw folder are not parsed and mapped onto calendars on my device. Any idea how I can go about fixing this issue?
Thanks!
回答1:
That seems to be a bit of an overkill. Why not add the event directly to your calendar via the following code? :
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);
来源:https://stackoverflow.com/questions/58632013/ics-file-is-not-parsing-and-retrieving-data-as-expected-in-android-using-ical