我正在创建目录并将下载的文件保存在位于SD卡中的文件夹中,但由于安全问题,我想在应用程序所在的位置创建文件夹,我该怎么办?
class DownloadFileFromURL extends AsyncTask<String, String, String>
{
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
File file = new File(Environment.getExternalStorageDirectory() +
"/download2/");
if (!file.exists()) {
file.mkdir();
}
File outputFile = new File(file, "CabApp.apk");
OutputStream output = new FileOutputStream(outputFile);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
Intent intent = new Intent(Intent.ACTION_VIEW);
File file2 = new File(Environment.getExternalStorageDirectory() +
"/download2/");
intent.setDataAndType(Uri.fromFile(new
File(Environment.getExternalStorageDirectory() + "/download2/" + "CabApp.apk")),
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
file2.delete();
}
}
答案 0 :(得分:0)
检查内部存储@
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
上面的链接中还有一个例子
引用文档
默认情况下,保存到内部存储的文件对您来说是私有的 应用程序和其他应用程序无法访问它们(也不能 用户)。当用户卸载您的应用程序时,这些文件是 除去。