Android - Copy Files (all at the same time) - android

I am currently working on a basic file browser for android. I have a working version for copying files, however as it works its way through directories it copies the files it finds. I want to change that so that I can find the total size of all files before starting to copy, as to help with a better progress bar.
If there is another way to find the total size of a directory and all its contents?
Here is my current version. I am having trouble changing this, I have tried using an arrayList however when I try to copy the files at the end, I think they are trying to copy in the wrong order.
public void copyDirectory(File sourceLocation , File targetLocation) throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists() && !targetLocation.mkdirs()) {
throw new IOException("Cannot create directory: " + targetLocation.getAbsolutePath());
}
String[] children = sourceLocation.list();
for (int i = 0; i < children.length; i++) {
copyDirectory(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]));
}
} else {
File directory = targetLocation.getParentFile();
if (directory != null && !directory.exists() && !directory.mkdirs()) {
throw new IOException("Cannot create directory: " + directory.getAbsolutePath());
}
FileInputStream in = new FileInputStream(sourceLocation);
FileOutputStream out = new FileOutputStream(targetLocation);
long fileLength = sourceLocation.length();
byte[] buf = new byte[1024];
long total = 0;
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
total += len;
publishProgress((int) (total * 100 / fileLength));
}
in.close();
out.close();
}
}
Solution
jtwigg's answer should also work. I just thought I would add the solution I found. Can't answer my own question so I will put it here.
Looping through all the files in the directory and keeping a running total seems to work. Although it requires looping first for the size and again to actually copy the files. Just call getDirectorySize() with the file or directory you wish to copy before calling copyDirectory().
private void getDirectorySize(File sourceLocation) throws IOException {
if (sourceLocation.isDirectory()) {
String[] children = sourceLocation.list();
for (int i = 0; i < children.length; i++) {
getDirectorySize(new File(sourceLocation, children[i]));
}
} else {
totalFileSize += sourceLocation.length();
}
}
The function will require the global long totalFileSize, and then all that is required is to replace:
publishProgress((int) (total * 100 / fileLength));
with:
publishProgress((int) (total * 100 / totalFileSize));

If I understand you correctly, you wish to find the total size of all the files in the directory and then copy them.
I would create another function, something like:
public void beginCopy(File source, File destination)
{
ArrayList<PendingFile> filesToCopy = new ArrayList<PendingFile>();
long totalSize = copyDirectory(source, destination, filesToCopy);
// totalsize now contains the size of all the files
// files to copy now contains a list of source and destination files
// now modifying your copy method we can copy all the files
long totalThusFar = 0;
for (PendingFile pending : filesToCopy)
{
FileInputStream in = new FileInputStream(pending.source);
FileOutputStream out = new FileOutputStream(pending.destination);
long fileLength = sourceLocation.length();
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
totalThusFar += len;
publishProgress((int) (total * 100 / totalsize));
}
in.close();
out.close();
}
}
you would need a PendingFile class/structure just to hold both source and destinations. You will add them to the ArrayList in your copy method like this:
public long copyDirectory(File sourceLocation , File targetLocation, ArrayList list) throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists() && !targetLocation.mkdirs()) {
throw new IOException("Cannot create directory: " + targetLocation.getAbsolutePath());
}
String[] children = sourceLocation.list();
long totalSize = 0;
for (int i = 0; i < children.length; i++) {
totalSize += copyDirectory(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]), list);
return totalSize;
}
} else {
File directory = targetLocation.getParentFile();
if (directory != null && !directory.exists() && !directory.mkdirs()) {
throw new IOException("Cannot create directory: " + directory.getAbsolutePath());
}
list.add(new PendingFile(sourceLocation, targetLocation));
return sourceLocation.length;
}
}
I wrote all this just now so It probably won't work straight away but I think you should be able to get it working with this. Goodluck!

Related

How to update UI with download progress when download is a runnable being executed in a ThreadpoolExecutor?

I am implementing a download manager in native android where a thread pool executor is used to implement parallel downloads. A runnable is where the actual download happens, which is being executed on the pool threads. How can I send the download progress from the runnable to the UI? In order to send broadcasts, I need to pass context into the runnable. Is that the appropriate way?
How can I handle pause/resume/cancel of download gracefully?
Right now the moment user taps the pause/cancel button the value is updated in the DB and while the Thread.CurrentThread().IsInterrupted condition in the runnable becomes valid I check the status in database and decide whether I need to delete the partially downloaded file (if its cancel).
Also, will it be possible to know when the download completes so that I can remove the future object from the list?
public class Downloadable : Java.Lang.Object, IRunnable
{
private readonly string _destination;
private readonly int _productId;
public Downloadable(int productId)
{
_productId = productId;
_destination = Utils.StoragePath() + productId + ".zip";
}
public void Run()
{
int count;
try
{
Response response = CloudService.GetCloud().GetDownLoadURL(_productId.ToString(), true).Result;
if (string.Equals(response.status, "error", StringComparison.OrdinalIgnoreCase) || string.Equals(response.status, "internalError", StringComparison.OrdinalIgnoreCase))
{
//send error
}
else
{
DownloadPath downloadPath = JsonConvert.DeserializeObject<DownloadPath>(response.data);
string offlineUrl = downloadPath.contentUrl.Offline;
if (string.IsNullOrWhiteSpace(offlineUrl))
{
//send error
}
else
{
File directory = new File(Utils.StoragePath());
if (!directory.Exists())
directory.Mkdirs();
URL url = new URL(offlineUrl);
HttpURLConnection connection = (HttpURLConnection)url.OpenConnection();
long total = 0;
File file = new File(_destination);
file.CreateNewFile();
if (file.Exists() && file.Length() > 0)
{
total = file.Length();
connection.SetRequestProperty("Range", "Bytes=" + total + "-");
}
connection.Connect();
int lenghtOfFile = connection.ContentLength;
BufferedInputStream bufferedInputStream = new BufferedInputStream(url.OpenStream());
FileOutputStream fileOutputStream = new FileOutputStream(_destination, true);
byte[] buffer = new byte[1024];
count = 0;
while ((count = bufferedInputStream.Read(buffer, 0, 1024)) != -1)
{
if (Thread.CurrentThread().IsInterrupted)
{
if (DBService.GetDB().GetStatus(_productId) == (int)IpcCommon.Enumerations.Status.DOWNLOAD)
file.Delete();
break;
}
total += count;
System.Console.WriteLine("__PROGRESS__ " + (int)((total * 100) / lenghtOfFile));
System.Console.WriteLine("__PROGRESS__ ID " + _productId);
//publishProgress("" + (int)((total * 100) / lenghtOfFile));
fileOutputStream.Write(buffer, 0, count);
}
fileOutputStream.Close();
bufferedInputStream.Close();
}
}
}
catch (System.Exception exception)
{
IpcCommon.App.Logger.Log("Downloadable - File Download", new System.Collections.Generic.Dictionary<string, string> { { "Error", exception.Message } });
}
}
}
Dictionary<int, IFuture> _runningTaskList = new Dictionary<int, IFuture>();
int noOfCores = Runtime.GetRuntime().AvailableProcessors();
LinkedBlockingQueue _taskQueue = new LinkedBlockingQueue();
_threadPoolExecutor = new ThreadPoolExecutor(noOfCores, noOfCores * 2, 1, TimeUnit.Minutes, _taskQueue);
IFuture future = _threadPoolExecutor.Submit(new Downloadable(productId));
_runningTaskList.Add(productId, future);

Download a file issue in android 2.3

I'm using this code to download from a URL ,it works great with android 4,but in the other hand it doesn't work with android 2.3. Can someone tell what have i done wrong ?
URL url = new URL(sUrl);
URLConnection connection = url.openConnection();
connection.connect();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(pathFolder+"/"+fileName);
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();
It works for me. Here is my method:
private boolean dowloadFile(String url, File saveFile) {
int BUFF_SIZE = 1024 * 1024; //1Mo
long length = 0;
long current = 0;
if(saveFile.exists())
current = saveFile.length();
try {
DefaultHttpClient http = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
if(current>0)
request.addHeader("Range", "bytes=" + current + "-");
HttpResponse response = http.execute(request);
if (response.getStatusLine().getStatusCode() != 200 && response.getStatusLine().getStatusCode() != 206) {
return false;
}
Header[] headers = response.getHeaders("Content-Range");
if(headers.length>0) {
String s = headers[0].getValue();
length = Integer.valueOf(s.subSequence(s.indexOf("/")+1, s.length()).toString());
} else {
Header[] headers2 = response.getHeaders("Content-Length");
if(headers2.length>0)
length = Integer.valueOf(headers2[0].getValue());
if(current>0) {
saveFile.delete();
current = 0;
}
}
BufferedInputStream ls = new BufferedInputStream(response.getEntity().getContent());
long nexttime = 0;
RandomAccessFile fos = new RandomAccessFile(saveFile, "rw");
fos.seek(current);
byte[] buffer = new byte[BUFF_SIZE];
while (current < length) {
boolean buffFull = false;
int currentBuff = 0;
int readSize = 0;
while (buffFull == false) {
readSize = ls.read(buffer, currentBuff, BUFF_SIZE - currentBuff);
if (readSize == -1)
buffFull = true;
else {
currentBuff += readSize;
if (currentBuff == BUFF_SIZE)
buffFull = true;
}
}
fos.write(buffer, 0, currentBuff);
current += currentBuff;
long time = SystemClock.elapsedRealtime();
if (nexttime < time) {
// Progress
nexttime = time + 1000;
}
}
fos.close();
// Progress Finish
} catch(Exception e) {
e.printStackTrace();
return false;
}
return true;
}
I hope I have helped you !

Cache and temp files/folders remove android

Hi friend thanks for previous replies,
i am facing problem in removing cache and temp files/folders,
what i require is to clean the whole device temp files and cache from one app which is mine app
but here i am able to clean only my apps cache , here is my code
private void mAppMethod(List<App> mApps) {
// TODO Auto-generated method stub
// File f = g
for (int i = 0; i < mApps.size(); i++) {
File dir = new File("/data/data/"+mApps.get(i).getPackageName().concat("/cache"));
Log.e("dir "+dir, "is directory "+dir.isDirectory());
int j = clearCacheFolder(dir, 10);
if (dir!= null && dir.isDirectory())
Log.e("j", "rff "+dir.delete());
System.out.println(j+" rff "+dir.delete());
}
and my clear cache method as under
static int clearCacheFolder(final File dir, final int numDays) {
int deletedFiles = 0;
if (dir!= null && dir.isDirectory()) {
// System.out.println("here"+dir.delete());
Log.e("here", "here "+dir.isDirectory());
try {
Log.e("here1", "here1"+dir.listFiles());
for (File child:dir.listFiles()) {
Log.e("here11", "here11");
//first delete subdirectories recursively
if (child.isDirectory()) {
Log.e("here111", "here111");
deletedFiles += clearCacheFolder(child, numDays);
Log.e("here1111", "here1111");
}
Log.e("here11111", "here11111");
//then delete the files and subdirectories in this dir
//only empty directories can be deleted, so subdirs have been done first
if (child.lastModified() < new Date().getTime() - numDays * DateUtils.DAY_IN_MILLIS) {
Log.e("here111111", "here111111");
if (child.delete()) {
Log.e("here1111111", "here1111111");
deletedFiles++;
Log.e("here11111111", "here11111111");
}
}
}
}
catch(Exception e) {
Log.e("TAG", String.format("Failed to clean the cache, error %s", e.getMessage()));
}
}
return deletedFiles;
}
please help how can i clear the whole device cache,here i am getting every apps cache location i.e. dir to cache of all apps in device but when i want to delete them it returns false
please help any help is appreciable
i am able to clear cache of one app which is the one i am running this code but not for other apps
thanks in advance
Try with this code
public void clearApplicationData() {
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if(appDir.exists()){
String[] children = appDir.list();
for(String s : children){
if(!s.equals("lib")){
deleteDir(new File(appDir, s));
Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s +" DELETED *******************");
}
}
}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
This will delete your all data,and cache files in the application.

comments need for exporting android Sqlite DB to CSV

I am trying to export the SQLite db of the app to CSV file and store in SD card. The exporting method was defined in DBAdapter class as ExportToCSV. I have check that the app create a SQLite database successfully. However, when I try to call ExportToCSV using onPause method in MainActivity, the app can not response to export the database. Can you give me any comments on how to correct this code? I will appreciate your help!
My code is as follows:
public void ExportToCSV(Cursor c, String fileName) {
int rowCount = 0;
int colCount = 0;
FileWriter fw;
BufferedWriter bfw;
File sdCardDir = Environment.getExternalStorageDirectory();
File saveFile = new File(sdCardDir, fileName);
try {
rowCount = c.getCount();
colCount = c.getColumnCount();
fw = new FileWriter(saveFile);
bfw = new BufferedWriter(fw);
if (rowCount > 0) {
c.moveToFirst();
// write the colume title
for (int i = 0; i < colCount; i++) {
if (i != colCount - 1)
bfw.write(c.getColumnName(i) + ',');
else
bfw.write(c.getColumnName(i));
}
// change the line
bfw.newLine();
// write data
for (int i = 0; i < rowCount; i++) {
c.moveToPosition(i);
Log.v("exporting data", "exportting" + (i + 1) + "line");
for (int j = 0; j < colCount; j++) {
if (j != colCount - 1)
bfw.write(c.getString(j) + ',');
else
bfw.write(c.getString(j));
}
// change line
bfw.newLine();
}
}
I use onPause in MainActivity.java to call ExportToCSV:
#Override
protected void onPause() {
super.onPause();
DBAdapter myDatabase=new DBAdapter(this);
myDatabase.open();
Cursor c=myDatabase.getAllgpspoint();
// this method was defined in DBAdapter.java and returned a Cursor
myDatabase.ExportToCSV(c, "IRI.csv");
myDatabase.close();
mSensorManager.unregisterListener(this);
}
You must close the file at the end.
bfw.close();

Android check download successful

For downloading stuff I work with the apache classes HTTPResponse HTTPClient etc.
I check for a valid download like this:
entity.writeTo(new FileOutputStream(outfile));
if(outfile.length()!=entity.getContentLength()){
long fileLength = outfile.length();
outfile.delete();
throw new Exception("Incomplete download, "+fileLength+"/"
+entity.getContentLength()+" bytes downloaded");
}
But it seems that the exception is never triggered. How to properly handle this? Is entity.getContentLength the length of the file on server or the amount of data received?
The file request should always come with a MD5 checksum. If you have an MD5 header then all you need to do is check that against the files generated MD5. Then your done, its better to do it this way as you can have a file with the same number of bytes but one byte gets garbled in transmission.
entity.writeTo(new FileOutputStream(outfile));
String md5 = response.getHeaders("Content-MD5")[0].getValue();
byte[] b64 = Base64.decode(md5, Base64.DEFAULT);
String sB64 = IntegrityUtils.toASCII(b64, 0, b64.length);
if (outfile.exists()) {
String orgMd5 = null;
try {
orgMd5 = IntegrityUtils.getMD5Checksum(outfile);
} catch (Exception e) {
Log.d(TAG,"Exception in file hex...");
}
if (orgMd5 != null && orgMd5.equals(sB64)) {
Log.d(TAG,"MD5 is equal to files MD5");
} else {
Log.d(TAG,"MD5 does not equal files MD5");
}
}
Add this class to your project:
public class IntegrityUtils {
public static String toASCII(byte b[], int start, int length) {
StringBuffer asciiString = new StringBuffer();
for (int i = start; i < (length + start); i++) {
// exclude nulls from the ASCII representation
if (b[i] != (byte) 0x00) {
asciiString.append((char) b[i]);
}
}
return asciiString.toString();
}
public static String getMD5Checksum(File file) throws Exception {
byte[] b = createChecksum(file);
String result = "";
for (int i = 0; i < b.length; i++) {
result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
}
return result;
}
public static byte[] createChecksum(File file) throws Exception {
InputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
MessageDigest complete = MessageDigest.getInstance("MD5");
int numRead;
do {
numRead = fis.read(buffer);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
fis.close();
return complete.digest();
}
}

Categories

Resources