(Android) 在内部存储中查找路径/Android

(Android) Find path /Android in internal storage

Android 中是否有方法 returns 内部存储上的数据路径?

我有 2 Android 部智能手机(三星 s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqliteDB:

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

问题是在 Samsung s2 中,我必须使用此路径获取数据库:

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

Instaed,在 s7 中我必须使用这个:

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

我想通过设备独立走这条路

你能帮帮我吗?

提前致谢。

我想你要找的就是这样的东西

String path = getFilesDir().getAbsolutePath();

一旦在不同的版本中改变你的逻辑.. 直到 android 4.xx 它以内存为 /storage/sdcard0/ 从 4.4 kitKat 开始,它将采用 /storage/emulated/0/

的路径

根据 android API 版本设计 multiple apk...

您的问题已解决..您可以使用该路径将其安装在 s2 和 S7 中...

基本上您想要访问应用程序外部目录。 IE。 "sdcard/Android/data/yourPackage/files"

你应该使用: getExternalFilesDir(null) Context 的方法 Class.

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

或者您可以直接获取文件实例,例如:

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

确保在清单中添加 WRITE_EXTERNAL_STORAGE 权限。

您可以在以下位置阅读更多相关信息: https://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)