Android HttpURLConnection未保存该文件
我正在尝试从flickr api下载图像,如下所示:Android HttpURLConnection未保存该文件,android,android-asynctask,Android,Android Asynctask,我正在尝试从flickr api下载图像,如下所示: //in main: new GetFlickr().execute(Lat, Long); // Subclass to download photos from flickr public class GetFlickr extends AsyncTask<Double, Double, String> { private String lat = "23.87";
//in main:
new GetFlickr().execute(Lat, Long);
// Subclass to download photos from flickr
public class GetFlickr extends AsyncTask<Double, Double, String> {
private String lat = "23.87";
private String lang = "81.55";
private String flickr_api = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=";
private String flickr_key = "Flickr_API_KEY";
private String flickr_opt = "&format=rest&accuracy=11&content_type=1&privacy_filter=1&geo_context=2";
private String urls = flickr_api + flickr_key + "&lat=" + lat + "&long=" + lang + flickr_opt;
private ProgressBar pBar;
@Override
protected void onPreExecute(){
super.onPreExecute();
pBar = new ProgressBar(getApplication());
Toast.makeText(getApplicationContext(), urls, Toast.LENGTH_LONG).show();
System.out.println(urls);
}
@Override
protected String doInBackground(Double... doubles) {
try{
URL url = new URL(urls);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
File file = new File(getApplicationContext().getFilesDir(),"flickr.xml");
FileOutputStream fOut = new FileOutputStream(file);
InputStream fInp = connection.getInputStream();
int totalSize = connection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while ((bufferLength = fInp.read(buffer)) > 0) {
fOut.write(buffer, 0, bufferLength);
//Toast.makeText(getApplicationContext(), Double.toString(bufferLength), Toast.LENGTH_LONG).show();
downloadedSize += bufferLength;
}
fOut.close();
} catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Integer... values) {
pBar.setProgress(values[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pBar.setVisibility(View.GONE);
}
}
我必须从中提取
farm
,secret
etc文件保存在getFilesDir()中。你为什么说它没有保存?我在看/stroge/emulator/。/
,它不在那里。getFilesDir()显示的路径是/data/user/0/com.example.helloworld/files/flickr.xml
,我无法访问它。您可以访问它。如果你能给它写信,你就可以从中阅读。此外,您没有说明如何访问该文件夹或使用什么。@blackapps:我想保存xml文件以解析其中的值(输出在主要问题中更新)。但是,问题是,如果我打开我的“文件”应用程序,进入Internal Storage->Android->data->com.example.helloworld
,那么flickr.xml
文件就不存在了。我应该在哪里找到它?如果你想用你自己的应用程序解析文件,你当然可以阅读文件。只需打开一个输入流并读取。
<rsp stat="ok">
<photos page="1" pages="1413" perpage="250" total="353095">
<photo id="49344451012" owner="111497945@N04" secret="7d073516fb" server="65535" farm="66" title="Etretat" ispublic="1" isfriend="0" isfamily="0"/>
<photo id="49343777058" owner="186211180@N06" secret="b937a40e99" server="65535" farm="66" title="vinhomes-smart-city-vuon-nhat-1" ispublic="1" isfriend="0" isfamily="0"/>
<photo id="49344446262" owner="57849799@N04" secret="a9e0d0a1ac" server="65535" farm="66" title="Solitario" ispublic="1" isfriend="0" isfamily="0"/>
<photo id="49344229736" owner="58472155@N05" secret="d4ff74d968" server="65535" farm="66" title="IMG_0565" ispublic="1" isfriend="0" isfamily="0"/>
<photo id="49343774068" owner="58472155@N05" secret="fac23b927c" server="65535" farm="66" title="IMG_0572" ispublic="1" isfriend="0" isfamily="0"/>
<photo id="49343773223" owner="50072010@N04" secret="25f12643e6" server="65535" farm="66" title="Aarti Ritual of the Sacred Ganges - Haridwar, India" ispublic="1" isfriend="0" isfamily="0"/>