我正在构建一个从url下载pdf格式的书籍并将其存储到设备中的Android应用程序,所以我编写了这个代码来创建名为“Mypdf”的文件夹并在其下载后保存文件,但是当我运行设备中的应用程序我只找到了Mypdf文件但没有任何pdf书籍,在我使用的代码下面
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
File file = new File(Environment.getExternalStorageDirectory()+"/Mypdf");
file.mkdir();
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("Mypdf/ "+booknameText+ ".pdf");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
Toast.makeText(getApplicationContext(), "The Book You downloaded had been saved into your SD Card " , Toast.LENGTH_LONG).show();
}
}
}
我使用了这个功能:
OutputStream output = new FileOutputStream("/sdcard/ "+booknameText+ ".pdf");
但是很多时候很多设备都没有SD卡,所以文件不会被保存,所以请你能帮帮忙吗