user1938357
user1938357

Reputation: 1466

get file path of sharedpreferences file

I am creating a sharedpreferences file in my android code. I then want to email that file in my code. For that I need to access the path of the sharedpreferences file. The code I am using is below. But it does not seem to work. I am able to open the email but there is no attachment there since I guess it could not get the file. Can someone suggest me any solution here.

File f = getDatabasePath("userPrefsFile.xml");
String filelocation=f.getAbsolutePath();
Intent email = new Intent(Intent.ACTION_SEND);
email.setType("application/xml");
String[] to = {"[email protected]"};
email.putExtra(Intent.EXTRA_EMAIL, to);
email.putExtra(Intent.EXTRA_STREAM,filelocation);
email.putExtra(Intent.EXTRA_SUBJECT,"test file send");
startActivity(Intent.createChooser(email, "Send email"));

Upvotes: 1

Views: 7952

Answers (1)

Simon Dorociak
Simon Dorociak

Reputation: 33495

So SharedPreferences files are located at directory

/data/data/your.package/shared_prefs

So you need to use path above.

Pseudo-code:

File root = new File("/data/data/your.package/shared_prefs");
if (root.isDirectory()) {
   for (File child: root.listFiles()) {
      Toast.makeText(this, child.getPath(), Toast.LENGTH_SHORT).show();
   }
}

Reason why you cannot use getDatabasePath() is that returns databases folder 1

/data/data/your.package/databases/


1 Same issue is also related to getFileStreamPath() method that returns

/data/data/your.package/files

Upvotes: 2

Related Questions