Created
October 10, 2012 05:07
-
-
Save Gopinathp/3863275 to your computer and use it in GitHub Desktop.
copy Android app's private files to sdcard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tringtringlabs.experiments; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.pm.PackageInfo; | |
import android.content.pm.PackageManager; | |
import android.content.pm.PackageManager.NameNotFoundException; | |
import android.os.Environment; | |
import android.widget.Toast; | |
public class Common { | |
public static void handleCopyFilesToSdCard(final Context context) { | |
Thread thread = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
File targetFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + context.getString(R.string.app_name) | |
+ "/"); | |
deleteAllFiles(targetFolder); | |
if (!targetFolder.exists()) { | |
targetFolder.mkdirs(); | |
} | |
File privateFilesRoot = new File("/data/data/" + context.getPackageName()); | |
copyFiles(privateFilesRoot, context); | |
} | |
}); | |
thread.setName("DevCopyFiles"); | |
thread.start(); | |
} | |
private static void deleteAllFiles(File targetFolder) { | |
File[] filesList = targetFolder.listFiles(); | |
if (filesList == null) { | |
return; | |
} | |
for (File file : filesList) { | |
if (file.isDirectory()) { | |
deleteAllFiles(file); | |
file.delete(); | |
} else { | |
file.delete(); | |
} | |
} | |
} | |
private static void copyFiles(File file, Context context) { | |
if (!file.exists()) { | |
return; | |
} | |
if (file.isDirectory()) { | |
File[] listFiles = file.listFiles(); | |
for (File afile : listFiles) { | |
copyFiles(afile, context); | |
} | |
} else { | |
File targetFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + context.getString(R.string.app_name) + "/" | |
+ file.getAbsolutePath().replace("/data/data/" + context.getPackageName(), "")); | |
File parentFile = targetFile.getParentFile(); | |
if (!parentFile.exists()) { | |
parentFile.mkdirs(); | |
} | |
if (!targetFile.exists()) { | |
try { | |
targetFile.createNewFile(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
try { | |
OutputStream oStream = new FileOutputStream(targetFile); | |
FileInputStream iStream = new FileInputStream(file); | |
byte[] buffer = new byte[512]; | |
int length = 0; | |
while ((length = iStream.read(buffer)) > 0) { | |
oStream.write(buffer, 0, length); | |
} | |
oStream.close(); | |
iStream.close(); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public static int getPackageVersion() { | |
App app = App.getApp(); | |
try { | |
PackageInfo packageInfo = app.getPackageManager().getPackageInfo(app.getPackageName(), PackageManager.GET_META_DATA); | |
return packageInfo.versionCode; | |
} catch (NameNotFoundException e) { | |
e.printStackTrace(); | |
} | |
return 0; | |
} | |
public static void comingSoon() { | |
Toast.makeText(App.getContext(), "Coming Soon", Toast.LENGTH_LONG).show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment