Android: Download Manager Request to Cache directory - android

I need to save sensitive files to a cached directory using the native Download Manager. A location that can not be discovered by the user. I can easily download to the user's external file system using DownloadManager.Request(). Although using setDestinationInExternalPublicDir(), or any other way to set destination, does not allow me to save to a cached directory. Am I missing something here?

The build in DownloadManager can not save files to internal directories. Only to external directories like the SD card and other public directories e.g. the video or photos folder.

You have to forget the Android build in Download manager and create your own manager. Then you can download to path: getCacheDir().getAbsolutePath();
Here is a sample code to download a file yourself without build-in manager
public String downloadFile(String fileURL, String fileName) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Log.d(TAG, "Downloading...");
try {
int lastDotPosition = fileName.lastIndexOf('/');
if( lastDotPosition > 0 ) {
String folder = fileName.substring(0, lastDotPosition);
File fDir = new File(folder);
fDir.mkdirs();
}
//Log.i(TAG, "URL: " + fileURL);
//Log.i(TAG, "File: " + fileName);
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setReadTimeout(30000);
c.connect();
double fileSize = (double) c.getContentLength();
int counter = 0;
while ( (fileSize == -1) && (counter <=30)){
c.disconnect();
u = new URL(fileURL);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setReadTimeout(30000);
c.connect();
fileSize = (double) c.getContentLength();
counter++;
}
File fOutput = new File(fileName);
if (fOutput.exists())
fOutput.delete();
BufferedOutputStream f = new BufferedOutputStream(new FileOutputStream(fOutput));
InputStream in = c.getInputStream();
byte[] buffer = new byte[8192];
int len1 = 0;
int downloadedData = 0;
while ((len1 = in.read(buffer)) > 0) {
downloadedData += len1;
f.write(buffer, 0, len1);
}
Log.d(TAG, "Finished");
f.close();
return fileName;
}
catch (Exception e) {
e.printStackTrace();
Log.e(TAG, e.toString());
return null;
}
}

Related

Saving an mp3 file to sd

i'm trying to download an mp3 file from an URL and save ut on the sd card in an music folder.
But no matter what I do it wont save it on the SD , it just downloads it and after while trying to find it it's no wher et obe found.
Here is my code in the async task class:
protected String doInBackground(String... params) {
try{
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
File sd = Environment.getExternalStorageDirectory();
File file = new File(sd, "TestSongs.mp3");
FileOutputStream outputStream = new FileOutputStream(file);
InputStream inputStream = connection.getInputStream();
int totalsize = connection.getContentLength();
int downloadSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0;
while((bufferLength = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0 , bufferLength);
downloadSize += bufferLength;
publishProgress("" + (int) ((downloadSize * 100) / totalsize));
}
outputStream.close();
}catch(Exception e) {
Log.d(TAG, e.getMessage());
}
return null;
}
Add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
somewhere inside the your manifest, outside the <application></application> tag

Android download even if does not exist

try {
URL url = new URL("http://URL/Dragonfly.db");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String[] path = url.getPath().split("/");
String _file = path[path.length - 1];
int lengthOfFile = c.getContentLength();
if(lengthOfFile > 0){ // Copy file if Length > 0
String PATH = db.DB_PATH; ;//Environment.getExternalStorageDirectory()+
Log.v("", "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
String fileName = "Dragonfly.db";
File outputFile = new File(file , fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
}else{
TestAdapter mDbHelper = new TestAdapter(getBaseContext());
mDbHelper.createDatabase();
}
} catch (IOException e) {
e.printStackTrace();
}
I use this code to update database, downloading a new one. but if i dont have a file on server, it replace the database i have for a new empty one (0bytes).
How can i download the file just if it exist on server?
Try to do a status response check:
int responseCode = c.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
{
// update database replacing the old one with the new one
} else {
// continue to use old database
}

Download .txt file from internet [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
File download with android
How do I download a .txt file from the internet at a given URL and save it to the phone in Android? It can be saved to either internal storage, SD storage, or anywhere really accessible for the app to use.
The URL I have been using for testing with loading URL WebViews is http://base.google.com/base/products.txt
This should do the trick. It downloads the file to sdcard/Download/products.txt. You can change it below.
try {
URL url = new URL("http://base.google.com/base/products.txt");
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
InputStream is = url.openStream();
File testDirectory = new File(Environment.getExternalStorageDirectory() + "/Download");
if (!testDirectory.exists()) {
testDirectory.mkdir();
}
FileOutputStream fos = new FileOutputStream(testDirectory + "/products.txt");
byte data[] = new byte[1024];
long total = 0;
int count = 0;
while ((count = is.read(data)) != -1) {
total += count;
int progress_temp = (int) total * 100 / lenghtOfFile;
/*publishProgress("" + progress_temp); //only for asynctask
if (progress_temp % 10 == 0 && progress != progress_temp) {
progress = progress_temp;
}*/
fos.write(data, 0, count);
}
is.close();
fos.close();
} catch (Exception e) {
Log.e("ERROR DOWNLOADING", "Unable to download" + e.getMessage());
}

Android: downloaded file size less than its actual size

I want to update my application automatically
This is the code i am using
public void Update(String apkurl){
try {
URL url = new URL(apkurl);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "DeliverReceipt.apk");
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();//till here, it works fine - .apk is download to my sdcard in download file
/*Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/apk/" + "DeliverReceipt.apk")), "application/vnd.android.package-archive");
startActivity(intent); //installation is not working
*/
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Update error!", Toast.LENGTH_LONG).show();
}
}
The downloaded file is just 20kb size, which is less than the original
how can i solve this problem?
thank you
*noted : if i try this url in browser it's work, an apk can be downloaded
You should probably flush before you close the file, using fos.flush()

Resume Download not working in android

This code for resuming download is not working properly in Android, although it works fine in a Java application. Here I am trying to download a zip file, and it will resume the download, but the net result is an invalid zip file.
BufferedInputStream in = null;
FileOutputStream fos = null;
BufferedOutputStream bout=null;
try {
downloaded=0;
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING){
File file=new File(DESTINATION_PATH);
if(file.exists()){
downloaded = (int) file.length();
}
}
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
connection.connect();
size=connection.getContentLength();
Dialog.setMax(size);
in = new BufferedInputStream(connection.getInputStream());
fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true);
bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
System.out.println(downloaded);
onProgressUpdate((int)(downloaded*100/size));
}
succes=true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
bout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Thanks.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int buf = 1024;
if (ISSUE_DOWNLOAD_STATUS.intValue() == ECMConstant.ECM_DOWNLOADING) {
File file = new File(DESTINATION_PATH);
if (file.exists()) {
downloaded = (int) file.length();
connection.setRequestProperty("Range",
"bytes=" + file.length() + "-");
}
} else {
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
progressBar.setMax(connection.getContentLength());
in = new BufferedInputStream(connection.getInputStream());
fos = new FileOutputStream(DESTINATION_PATH, downloaded == 0 ? false : true);
bout = new BufferedOutputStream(fos, buf);
byte[] data = new byte[buf];
while ((int x = in.read(data, 0, buf)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
progressBar.setProgress(downloaded);
}
Your zip file is corrupted because you think that the stream resumes from the range byte that you specified. It actually streams from the beginning again, and so you have a file bigger than the original. Long story short, your server does not support the range property.

Categories

Resources