我正在编写一个包含大约500个图像的android应用程序。有些事情让我担心,我不想上网。
1-应用程序的大小会很大,安装时有没有将图像移动到sd卡?有些设备的手机空间可能没有这么大。
2-我应该为hdpi、ldpi和mdpi制作3个图像吗?
您可以将图像放在资产文件夹中。如果你想将图像从资产转移到SD卡,那么你不能这样做。但你可以用一种方法。你把你的图像放在服务器上,当你第一次打开应用程序时,你可以下载并保存在SD卡中,然后从那里访问。
- 是的,它会很大。不,您不能将它们从包中删除
- 不可以,您只能制作
hdpi
图像。安卓系统会自动缩放它们(这可能会让应用程序的速度慢一点)
建议-使用互联网。由于用户有互联网可以下载你的应用程序,他可以在第一次启动时等待下载资源。此外,它还使您能够通过在线配置添加/删除文件。想象一下,如果你必须添加1个图像并上传新版本,这意味着用户将不得不再次下载相同的巨大软件包。
我有一个类似的要求-在应用程序中包含一堆图像,但在我的情况下,任何用户或应用程序都必须可以访问图像,而不仅仅是打开图像的应用程序。我将它们存储在res/raw文件夹中,并在启动时将它们复制到用户空间:
private void loadCopyResources() {
// copy resources to space any activity can use
String sourceName;
String resourceName;
String fileName;
int resource;
String typeName = sourceSink.Types.photo.toString();
for (sourceSink.Sources source: sourceSink.Sources.values() ){
for (int i = 0; i< photoFileCount; i++) {
sourceName = source.toString();
resourceName = sourceName + "_" + typeName + (i+1); // i.e. dropbox_photo2
fileName = resourceName + ".jpg"; // files requires extension
resource = getResources().getIdentifier(resourceName, "raw", "com.example.myapp");
createExternalStoragePublicFile(typeName,fileName, resource); // copy it over
}
}
}
void createExternalStoragePublicFile(String fType, String fname, int res ) {
// Create a path where we will place our picture in the user's
// public pictures directory. Note that you should be careful about
// what you place here, since the user often manages these files. For
// pictures and other media owned by the application, consider
// Context.getExternalMediaDir().
File path = null;
if (((fType.equals(sourceSink.Types.photo.toString())) || (fType.equals(sourceSink.Types.file.toString())) ) ){
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
}
if (fType.equals(sourceSink.Types.music.toString())) {
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MUSIC);
}
if (fType.equals(sourceSink.Types.video.toString())) {
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES);
}
File file = new File(path, "/" + fname);
try {
// Make sure the Pictures directory exists.
path.mkdirs();
// Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does
// no error checking, and assumes the picture is small (does not
// try to copy it in chunks). Note that if external storage is
// not currently mounted this will silently fail.
InputStream is = getResources().openRawResource(res);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
scanMedia(file);
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
sourceSink,我没有包括在内,只是我需要复制的文件名和文件类型的列表。