gpt4 book ai didi

java - 为什么 FileNotFound 仅在 Ice Cream Sandwich 和 JellyBean 中被抛出

转载 作者:搜寻专家 更新时间:2023-11-01 09:06:45 24 4
gpt4 key购买 nike

我正在尝试使用 ical4j 库解析一个 icalendar 文件 (.ics),它在除 IceCreamSandwich 和 JellyBean 之外的所有 Android 版本上都能正常工作。

有人能告诉我为什么它只在 ICS 和 JB 中抛出 FileNotFound 错误,而在其他版本的 android 中没有吗?

这是我的代码:

public class MainActivity extends Activity {
String foo = null;
TextView TextView = null;

String fileName = "ical.ics";
String URL = "https://www.google.com/calendar/ical/m0es4hhj4g9d69ibak88tvoup0%40group.calendar.google.com/public/basic.ics";
StringBuilder b = new StringBuilder();

@Override
public void onCreate(Bundle savedInstanceState) {

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView = (TextView)findViewById(R.id.Hello_World);

new Download().execute();




}


final class Download extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute(){

TextView.setText("Downloading");

}

@Override
protected Void doInBackground(Void... arg0) {


try {
URL url = new URL(URL);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();






FileOutputStream fos = openFileOutput(fileName, MainActivity.MODE_PRIVATE);

InputStream is = c.getInputStream();


byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}
fos.close();
is.close();
} catch (IOException e) {
Log.d("log_tag", "Error: " + e);
}


return null;
}


@Override
protected void onPostExecute(Void Result) {


TextView.setText("Saved...Loading Data");
new Loadicaldata().execute();

}


}



final class Loadicaldata extends AsyncTask<Void, Void, Void> {



@Override
protected Void doInBackground(Void... arg0) {


FileInputStream fis = null;
try {
fis = openFileInput(fileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_UNFOLDING, true);
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION, true);
CalendarBuilder builder = new CalendarBuilder();



try {
Calendar calendar = builder.build(fis);
b.append(calendar.getProperty("X-WR-CALNAME").getValue());
for (Object event : calendar.getComponents(Component.VEVENT)) {
if (((VEvent) event).getSummary() != null) {
b.append("\n\n");
b.append(((VEvent) event).getSummary().getValue());
b.append(": ");
b.append(((VEvent) event).getStartDate().getDate());

}
}


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



return null;
}

@Override
protected void onPostExecute(Void Result) {


TextView.setText(b.toString());

}

}

另外,我注意到如果我使用 Calendar.load(URL url) 它工作正常。所以是文件的保存和加载出了问题。

最佳答案

尝试删除

c.setDoOutput(true);

(如本 blog post 所建议)

关于java - 为什么 FileNotFound 仅在 Ice Cream Sandwich 和 JellyBean 中被抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11384908/

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