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

Java 复制到其他应用程序未受保护的存储Android,java,android,storage,sd-card,Java,Android,Storage,Sd Card,我正在尝试将文件wirte到内部存储实际上是到其他应用程序未受保护的存储我正在使用的代码工作它只是在我要复制到/android/data/com.some.app时不工作。这可以做到吗,还是我必须让应用程序使用根访问 谢谢 public class tools extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我正在尝试将文件wirte到内部存储实际上是到其他应用程序未受保护的存储我正在使用的代码工作它只是在我要复制到/android/data/com.some.app时不工作。这可以做到吗,还是我必须让应用程序使用根访问

谢谢

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

你会得到哪一个错误(张贴日志)。如果不确定,我认为它不会被发现。我认为每个应用程序都对其未受保护的存储具有文件访问权限。您需要更改应用程序A中的文件访问权限,以允许其他用户访问此文件,然后才能从应用程序B访问。外部应用程序数据目录(即应用程序通过的数据目录)也应可为每个其他应用程序写入(虽然Android 4.4确实更改了权限模型,但它可能不再工作,或者将来可能不再工作-请参阅
getExternalFilesDir
的说明)。另一方面,请注意:使用
AsyncTask
,因为UI将冻结,“这可能需要很长时间”文本在处理完成后才会显示。@henry我没有收到log cat错误,我收到了这个“@boris”,我想做的是让我的应用程序替换游戏图片(如纹理包)因此,我无法在另一个应用程序上真正设置权限,我知道这可以在文件管理器中完成。“@zapl”感谢您提供有关使用AsyncTask的技巧,这很有帮助。我还没有时间查看“getExternalFilesDir”