-1

我尝试将文件从其他包“/data/data/targetPackage”复制到“/data/data/myPackate”,但它不起作用。我有请求su权限

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

请帮我

4

1 回答 1

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

}

于 2012-12-18T10:03:49.120 回答