1
AsyncTask를 사용하여 URL에서 파일을 다운로드합니다. 파일이 저장되고 내 sdcard에서 볼 수 있습니다. 내 응용 프로그램에서 나는 거기에 오류가 따르고이 다운로드 된 후이 파일을 열 싶지만 :AsyncTask로 URL에서 다운로드 한 파일을 열려고 할 때 ENOENT (해당 파일이나 디렉토리가 없음)
java.io.FileNotFoundException: /mnt/sdcard/XML/zurt.xml: open failed: ENOENT (No such file or directory)
내가 다운로드 및 파일을 여는 사이에 지정된 시간 동안 대기해야합니까? 문제가 무엇입니까?
나는이 두 권한이 :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" >
</uses-permission>
내 AsyncTask를 :
/**
* Download XML from URL async
*/
// usually, subclasses of AsyncTask are declared inside the activity class.
// that way, you can easily modify the UI thread from here
private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
try {
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100% progress bar
int fileLength = connection.getContentLength();
// create a File object for the parent directory
File xmlDirectory = new File(Environment.getExternalStorageDirectory()+ FileSeperator+"XML"
+FileSeperator);
// have the object build the directory structure, if needed.
xmlDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(xmlDirectory, Name+FileExtension);
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(outputFile);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
publishProgress((int) (total * 100/fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
System.out.println(e);
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}
protected void onPostExecute() {
mProgressDialog.dismiss();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
mProgressDialog.setProgress(progress[0]);
}
}
코드는 저장된 파일을 열려고이 :
/**
* XML Parser
* */
private ArrayList<Datapoint> parseXML() {
try {
Log.w("AndroidParseXMLActivity", "Start");
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
File file = new File(Environment.getExternalStorageDirectory()+ FileSeperator+"XML"
+FileSeperator+ Name + FileExtension);
XMLContentHandler myXMLHandler = new XMLContentHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(new InputStreamReader(new FileInputStream(file))));
itemsList = myXMLHandler.getItemsList();
Log.w("AndroidParseXMLActivity", "Done");
}
catch (Exception e) {
Log.w("AndroidParseXMLActivity",e);
}
return itemsList ;
}
다운로드 후 파일에 액세스하는 코드를 표시하십시오. 파일을 생성 한 후 –
을 작성한 다음,이 print 문을 추가하여 쓰고있는 파일이 예상 파일인지 확인하십시오 :'Log.i ("FILE", outputFile.getName());' – Phil
@Raghav 메인 포스트. –