9

Is there a method in Android that returns the data path on internal storage?

I have 2 Android smartphone (Samsung s2 and s7 edge) where i installed an application. I want to take a sqliteDB situated in this path:

/Android/data/application.most/files/most_db

The problem is that in Samsung s2 i must use this path to take the DB:

private static final String db_path = "/storage/sdcard0/Android/data/org.application/files/application_db";

Instaed, in s7 i must use this:

private static final String db_path = "/storage/emulated/0/Android/data/application.most/files/application_db";

I want to take this path independently by device.

Can you help me?

Thanks in advance.

Federico
  • 143
  • 1
  • 1
  • 10

3 Answers3

18

I think that what you're looking for is something like that

String path = getFilesDir().getAbsolutePath();
antonis_st
  • 468
  • 3
  • 16
2

Once change your Logic in different versions.. till android 4.xx it take internal memory as /storage/sdcard0/ where as from 4.4 kitKat it will take path as /storage/emulated/0/

Design multiple apk depending on android API versions...

your problem is solved.. you can install it in s2 with that path and S7 with this path...

Don't Be negative
  • 1,215
  • 3
  • 19
  • 46
0

Basically you want to Access Applications External Directory. ie. "sdcard/Android/data/yourPackage/files"

You should use: getExternalFilesDir(null) method of Context Class.

private static final String db_path = getExternalFilesDir(null).getAbsolutePath() + "/application_db";

OR you can Directly get File Instance like:

File file = new File(getExternalFileDir(null),"application_db");

Make Sure you add WRITE_EXTERNAL_STORAGE Permission in the Manifest.

You can read more about this at : https://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

Sahil Sharma
  • 117
  • 1
  • 4