I try to copy a file from other package "/data/data/targetPackage" to "/data/data/myPackate"
but it is not working. I have request supermission
p = Runtime.getRuntime().exec("su");
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("chmod 777 /data/data/targetPackage/databases"+"\n");
// Close the terminal
os.writeBytes("exit\n");
os.flush();
try
{
p.waitFor();
if (p.exitValue() != 255)
{
// TODO Code to run on success
toastMessage("root");
//accsessDB();
}
else
{
// TODO Code to run on unsuccessful
toastMessage("not root");
}
}
catch (InterruptedException e)
{
// TODO Code to run in interrupted exception
toastMessage("not root");
}
and this code copy
File f=new File("/data/data/targetPackage/databases/targetFile");
InputStream input = new FileInputStream(f);
new File("/data/data/myPackate/databases").mkdir();
File f2=new File("/data/data/myPackate/databases/targetFile");
OutputStream output = new FileOutputStream(f2,true);
byte[] buf= new byte[1024];
toastMessage(Integer.toString(input.read(buf)));
int len;
while((len=input.read(buf))>0)
{
output.write(buf, 0,len);
}
output.close();
input.close();
please help me
Try this it'll work..
private void copyFile()
{
try{
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
} else {
File file = new File(Environment.getExternalStorageDirectory()
+File.separator
+"App"
+File.separator
+"app"
+File.separator
+"images"
+File.separator
); //file name
file.mkdirs();
File source= new File(filePath);
File destination= new File(file,nameimage);
newloc=Environment.getExternalStorageDirectory()+"/"+"App"+"/"+"app"+"/"+"images"+"/";
newimgloc=Environment.getExternalStorageDirectory()+"/"+"App"+"/"+"app"+"/"+"images"+"/"+nameimage;
if (source.exists()) {
FileChannel src = new FileInputStream(source).getChannel();
FileChannel dst = new FileOutputStream(destination).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
File dir = Environment.getExternalStorageDirectory();
if(dir.exists()){
toname=generatePin();
File from = new File(newloc,nameimage);
File to = new File(newloc,toname+".jpg");
if(from.exists())
from.renameTo(to);
filePath=newloc+toname+".jpg";
nameimage=toname+".jpg";
filedel();
}
}
}
}catch (Exception e) {
System.out.println(e);
Toast.makeText(getApplicationContext(), "Catch",Toast.LENGTH_SHORT).show();
}
}
Related
I am trying to copy two files from assets folder to external storage, one is a text based and another one is a rar file(60 kb). But when i open the rar file in file manager, it says "wrong header". The size of the rar file which i get is 16kb.
private void copyAsset() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(SKETCH_FILE);
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/Ardumation");
if (!dir.exists()) {
//System.out.println("creating directory: " + directoryName);
dir.mkdir();
}
//File outFile = new File(getExternalFilesDir(null), SKETCH_FILE);
File sketchFile = new File(dir, SKETCH_FILE);
File libraryFile = new File(dir, LIBRARY_FILE);
if (!sketchFile.exists()){
out = new FileOutputStream(sketchFile);
copyFile(in, out);
}
if (!libraryFile.exists()){
out = new FileOutputStream(libraryFile);
copyFile(in, out);
}
} catch(IOException e) {
Log.e(TAG, "Failed to copy asset file: ", e);
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// NOOP
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
Now what is wrong..?
I have a sqlite database in my assets folder. I want to export that file. So I do this:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
//
File direct = new File(Environment.getExternalStorageDirectory()
+ "/Exam Creator");
if (!direct.exists())
{
if (direct.mkdir())
{
// directory is created;
}
}
exportDB();
// importDB();
}
private void exportDB()
{
// TODO Auto-generated method stub
try
{
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite())
{
dbopenhelper.copyDataBase();
String currentDBPath = dbopenhelper.DB_PATH
+ dbopenhelper.DATABASE_NAME;
String backupDBPath = "/BackupFolder/Prayers.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getBaseContext(),
backupDB.toString() + "SUCCESS", Toast.LENGTH_LONG)
.show();
}
} catch (Exception e)
{
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}
}
to get the path of my currentDB, I first copy the database in my project using this method in my DBOpenHelper and by calling it in export method as you can see above.
public static void copyDataBase()
{
try
{
InputStream input = context.getAssets().open(DATABASE_NAME);
FileOutputStream output = new FileOutputStream(DB_PATH
+ DATABASE_NAME);
byte[] data = new byte[1024];
int buffer;
while ((buffer = input.read(data)) != -1)
{
output.write(data, 0, buffer);
}
output.flush();
output.close();
input.close();
} catch (IOException ex)
{
Log.e(LOGTAG, "Error in copying database...");
}
}
but when I run it, I get Filenotfound exception. I debuged it and saw that after this line:
FileChannel src = new FileInputStream(currentDB).getChannel();
it goes to catch block. Does anyone know what is wrong?
Thanks in advance
this code does not copy .db file on sdcard while my phone is rooted
try {
String comando = "cp -r /data/data/com.whatsapp/databases/msgstore.db /storage/sdcard0/tmp";
Process suProcess = Runtime.getRuntime().exec("su");
System.out.println(">>>>"
+ Environment.getExternalStorageDirectory());
DataOutputStream os = new DataOutputStream(
suProcess.getOutputStream());
os.writeBytes(comando + "\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
try {
int suProcessRetval = suProcess.waitFor();
if (255 != suProcessRetval) {
//
System.out.println(">>>>> done >>>>");
} else {
//
System.out.println(">>>>> not done >>>>");
}
} catch (Exception ex) {
Log.e("ERROR-->", ex.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
Copy database from /data/data folder to sdcard :
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//com.example.usarmy//databases//usarmy_db.sqlite";
String backupDBPath = "backdatabase.sqlite";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
System.out.println("error in data base copy:"+e);
}
I have been trying to export my database file into the external memory of my android phone by using
private final String DB_NAME = "MemberData";
private final String TABLE_NAME = "MemberDB";
//Get a reference to the database
File dbFile = this.getDatabasePath(DB_NAME);
//Get a reference to the directory location for the backup
File exportDir = new File(Environment.getExternalStorageDirectory(), "myAppBackups");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File backup = new File(exportDir, dbFile.getName());
//Check the required operation String command = params[0];
//Attempt file copy
try {
backup.createNewFile();
fileCopy(dbFile, backup);
} catch (IOException e) {
/*Handle File Error*/
}
private void fileCopy(File source, File dest) throws IOException {
FileChannel inChannel = new FileInputStream(source).getChannel();
FileChannel outChannel = new FileOutputStream(dest).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null) inChannel.close();
if (outChannel != null) outChannel.close();
}
}
It managed to create a directory name "myappsbackup" but my database couldnt be copied over. it is always size 0 and my tables are missing. Is there something wrong with my method of copying?
Here is the code I use to write or backup my SQLite db to the sdcard.
try {
db.open();
File newFile = new File("/sdcard/Your File Name Here");
InputStream input = new FileInputStream(
"/data/data/com.packageNameHere/databases/DB Name Here");
OutputStream output = new FileOutputStream(newFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
db.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Can anyone kind enough show me how to copy files from my app assets folder to /system folder? I know how to get root access and all. For example: I want to copy file from "/assets/lib/libs.so" and check if this file already exist, if it does replace it to new "/system/lib/libs.so".
try this:
try {
File from = new File( "/assets/lib/libs.so" );
File to = new File( "/system/lib/libs.so" );
if( from.exists() && to.exists() ) {
FileInputStream is = new FileInputStream( from );
FileOutputStream os = new FileOutputStream( to );
FileChannel src = is.getChannel();
FileChannel dst = os.getChannel();
dst.transferFrom( src, 0, src.size() );
src.close();
dst.close();
is.close();
os.close();
}
} catch( Exception e ) {
}
This will check if your file exists, delete it and then copy everthing that is in assets.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File exists = new File("/system/lib/libs.so");
if(exists.exists()){
exists.delete();
CopyAssets();
}else{
CopyAssets();
}
}
private void CopyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", e.getMessage());
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
out = new FileOutputStream("/system/lib/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {
Log.e("tag", e.getMessage());
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
Edit:
Declare this permission in your manifest file for filesystems.
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>