我怎样才能在数据目录中复制文件

时间:2015-11-26 03:46:30

标签: android

我想将文件从资产复制到其他应用程序数据目录。我也给它了一个root权限。但是这段代码不起作用。在extenal存储目录上工作。但不在数据目录中复制文件。

public void onClick(View arg1){


                    String command[] = { "su", "-c", "ls", "/data" };
                    Shell shell = new Shell();
                    String text = shell.sendShellCommand(command);


                    if (new File((Object)Environment.getDataDirectory() + "/data/com.my/shared_pref/com.myxml").exists()) {
                Toast.makeText(getApplicationContext(),"copied",Toast.LENGTH_LONG).show();



                        MainActivity.this.copyAssets();
                    }
                    else{
                        Toast.makeText(getApplicationContext(),"error! copy failed ",Toast.LENGTH_LONG).show();




    private void copyAssets()
    {
        AssetManager assetManager = getAssets();
        String[] files = null;
        InputStream in = null;
        OutputStream out = null;
        String filename =
            "com.my.xml" ;
        try
        {
            in = assetManager.open( filename);
            out = new FileOutputStream((Environment.getDataDirectory().toString() +"/data/com.my/shared_pref/" + filename)); 

            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        }
        catch (IOException e)
        {
            Log. e ( "tag" , "Failed to copy asset file: " , e);
        }
    }
    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);
        }
    }

如果有人知道该怎么做? Pleaase建议我。!!

感谢。!!

2 个答案:

答案 0 :(得分:0)

使用此代码。我在我的项目中使用了这个代码,在网上得到它,它可以工作。

 private void copyDataBase() throws IOException
    {
        //Open your local db as the input stream
        InputStream myInput = _context.getAssets().open(DB_NAME);

        // Path to  db
        String outFileName = DB_PATH + DB_NAME;

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0)
        {
            myOutput.write(buffer, 0, length);
        }
        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

答案 1 :(得分:0)

这正是您所需要的。此代码将sample.apk从资产复制到应用程序数据目录。您可以将路径更改为您想要的任何位置。

 private String copyAssets() {
    AssetManager assetManager = getActivity().getAssets();
    InputStream in = null;
    OutputStream out = null;
    String filename = "sample.apk";
    String path = Environment.getExternalStorageDirectory()
            + "/Android/data/"
            + getActivity().getPackageName()
            + "/files";
    try {
        in = assetManager.open("files/" + filename);

        File outFile = new File(path);

        if (!outFile.exists()) {
            outFile.mkdirs();
        }
        out = new FileOutputStream(outFile + "/" + filename);
        copyFile(in, out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return path + "/" + filename;
}

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