Write a private access file to another app's files directory

前端 未结 4 676
花落未央
花落未央 2020-12-31 19:57

The two apps have the same sharedUserId. When I use this code in app1

context.openFileOutput(\"/data/data/org.me.app2/files/shared-data.dat\", MODE_PRIVATE)
         


        
4条回答
  •  一生所求
    2020-12-31 20:42

    First of all, NEVER use a full path to internal storage like /data/data. Let the operating system give you the path (for example, via Context.getFilesDir() or Environment.getExternalStorageState()). Don't make assumption on where the data is.

    Secondly - you already are doing that! Unlike File, Context.openFileOutput already prepends /data/data/[package] to your path, so you don't need to specify that. Just specify the file name.

    If you really feel that it's safe and necessary, and if both apps share the same user ID using android:sharedUserId in the manifest, you can get a context of the other app by using Context.createPackageContext() and use CONTEXT_RESTRICTED, then use openFileOutput with only the file name.

提交回复
热议问题