0
我有一个AsyncTask将文件下载到我的android应用程序的内部存储。我想稍后阅读这个文件,但我不知道我该怎么做。这是我的AsyncTask's代码:从内部存储读取文件
private class GetFile extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(ComprobadorDos.this);
pDialog.setMessage("Por favor espere...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
try {
File fileEvents = new File(context.getFilesDir().getAbsolutePath() + "/agenda_hoy.json");
if (fileEvents.exists()) {
fileEvents.delete();
}
String buffer;
URLConnection conn = new URL(CALLBACK_URL).openConnection();
conn.setUseCaches(false);
conn.connect();
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(isr);
FileOutputStream fos = context.openFileOutput("agenda_hoy.json", Context.MODE_PRIVATE);
while ((buffer = br.readLine()) != null) {
fos.write(buffer.getBytes());
}
fos.close();
br.close();
isr.close();
} catch (Exception e) {
Log.d(LOG_TAG, e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
我希望这是一个很好的代码,如果有人知道一个更好的做法,请告诉我,我怎么能做到这一点。
嗨!我的问题的代码不起作用,你知道为什么吗?谢谢 –
如果你向我解释这是什么意思“不起作用”,也许我可以帮助你 – Apperside