复制到其他应用程序不受保护的存储Android

时间:2014-03-04 09:06:21

标签: java android storage sd-card

我正在尝试将文件传输到内部存储器实际上到其他应用程序不受保护的存储我正在使用的代码工作它只是在我想复制到/android/data/com.some.app时不起作用可以这样做,还是我必须让应用程序使用root访问权限

由于

public class tools extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tools);

    }


    public void installflat(View view)
    {   Toast.makeText(tools.this, "This could take a long time", Toast.LENGTH_LONG).show();    
        File direct = new File(Environment.getExternalStorageDirectory()  + "/android" + "/data" + "/com.some.app" +"/");
        if (!direct.exists())
        { 
            if (direct.mkdir())
            { //directory is created;
            }}
        installflat_pt2();
        Toast.makeText(tools.this, "try it now", Toast.LENGTH_LONG).show(); 

    }
    private void installflat_pt2()
    {
        AssetManager assetManager = getAssets();
        String[] files = null;
        try
        {
            files = assetManager.list("tools");
        }
        catch (IOException e)
        {
            Log.e("tag", e.getMessage());
        }

        for (String filename : files)
        {
            System.out.println("File name => " + filename);
            InputStream in = null;
            OutputStream out = null;
            try
            {
                in = assetManager.open("tools/" + filename);   // if files resides inside the "Files" directory itself
                out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() + "/android" + "/data" + "/com.some.app" + filename);
                copyFile(in, out);
                in.close();
                in = null;
                out.flush();
                out.close();
                out = null;
            }
            catch (Exception e)
            {
                Log.e("tag", e.getMessage());

            }       
        }
    }

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

0 个答案:

没有答案