简体   繁体   中英

how can i copy file in data directory

i want to copy file from asset to other application data directory.i have also given root acess to it.but this code doesn't work.its work on extenal storage directory .but not copy file in data directory..

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

if anyone knows how to do it? Pleaase suggest me.!!

Thanks.!!

Use this code. I used this code in my project, Got it on net somewhare, it works.

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

This is exactly what you need. This code copies sample.apk from assets to the application data directory. You can change the path to anywhere you want.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM