问题描述
我写一个Android应用程序,存储了大量的媒体文件。他们不是类型(以及太多太多),以扰乱用户的通知或其他媒体目录,但他们也必须由用户更新,所以我不能把他们的资源。我可以用getExternalFilesDir,以取得该SD卡的路径,但我只想要做的,如果应用程序本身被安装在SD卡。如果应用程序被安装在内部,我希望把媒体在内部存储器中。
I am writing an android app that stores a lot of media files. They are not the type (and are far too many) to clutter up the users notification or other media directories, but they also must be user-updatable, so I can't put them in the resources. I can use getExternalFilesDir to get a path on the sdcard, but I only want to do that if the app itself is installed on the sdcard. If the app is installed internally, I want to put the media in the internal memory.
那么,如何能确定我的应用程序是在内部或外部存储器运行?
So how can I determine if my app is running in internal or external memory?
推荐答案
您可以使用PackageManager得到ApplicationInfo,并从那里检查标志的FLAG_EXTERNAL_STORAGE。
You could use PackageManager to get the ApplicationInfo, and from there check the "flags" for FLAG_EXTERNAL_STORAGE.
http://developer.android.com/intl/de/reference/android/content/pm/ApplicationInfo.html#FLAG_EXTERNAL_STORAGE
下面是一个简单的例子,我作了演示:
Here's a quick example I made to demonstrate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PackageManager pm = getPackageManager();
try {
PackageInfo pi = pm.getPackageInfo("com.totsp.helloworld", 0);
ApplicationInfo ai = pi.applicationInfo;
// this only works on API level 8 and higher (check that first)
Toast
.makeText(
this,
"Value of FLAG_EXTERNAL_STORAGE:"
+ ((ai.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE),
Toast.LENGTH_LONG).show();
} catch (NameNotFoundException e) {
// do something
}
}
不过,根据您的情况(你是否拥有所有的媒体的前面,或者用户获取/创建它,因为他们使用的应用程序),您可能希望把它放在外部存储不管。大尺寸的内部应用程序是令人难以接受,许多用户(以及大量的内部媒体可能会使其巨大的)。
Still, depending on your situation (whether or not you have all the "media" up front, or the user gets/creates it as they use the app), you may want to put it on the external storage regardless. A large size internal app is frowned upon by many users (and a lot of internal media would probably make it huge).
这篇关于Android 2.2的 - 如何检测,如果我安装在SD卡或不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!