簡體   English   中英

我如何復制數據目錄中的文件

[英]how can i 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);
        }
    }

如果有人知道怎么做? 請建議我!!

謝謝。!!

使用此代碼。 我在項目中使用了此代碼,並以某種方式在線獲取了它的效果。

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

這正是您所需要的。 該代碼將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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM