2012-12-18 63 views
-1

我嘗試從其他軟件包「/ data/data/targetPackage」複製到「/ data/data/myPackate」 ,但它不起作用。我請求su許可android複製文件不起作用?

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"); 
} 

這個代碼複製

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(); 

請幫我

+0

定義「not working」 – njzk2

+0

它不起作用,因爲您在toastMessage行開始複製之前先讀取第一批。 – njzk2

回答

1

試試這個,它會工作..

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(); 
    } 

}

+0

我嘗試你的代碼不工作:(你有opinoin? – Mayday