android在何处存储大量图像以及如何在安装时移动图像
我正在编写一个android应用程序,其中包含大约500幅图像。 有些事情让我担心,我不想上网 1-应用程序的大小将非常大,安装时是否需要将图像移动到sd卡?有些设备可能没有足够的手机空间 2-我应该为hdpi、ldpi和mdpi制作3张图像吗android在何处存储大量图像以及如何在安装时移动图像,android,image,file,move,Android,Image,File,Move,我正在编写一个android应用程序,其中包含大约500幅图像。 有些事情让我担心,我不想上网 1-应用程序的大小将非常大,安装时是否需要将图像移动到sd卡?有些设备可能没有足够的手机空间 2-我应该为hdpi、ldpi和mdpi制作3张图像吗 是的,它会很大。不,您不能从包中删除它们 不可以,您只能制作hdpi图像。Android将自动扩展它们(这可能会使应用程序的速度减慢一点) 建议-使用互联网。由于用户可以在internet上下载您的应用程序,他可以在第一次启动时等待下载资源。它还使您能够
hdpi
图像。Android将自动扩展它们(这可能会使应用程序的速度减慢一点)建议-使用互联网。由于用户可以在internet上下载您的应用程序,他可以在第一次启动时等待下载资源。它还使您能够通过在线配置添加/删除文件。试想一下,如果您必须添加1个图像并上载新版本,这意味着用户必须再次下载相同的大型软件包。您可以将图像放在资产文件夹中。如果您想将图像从资产传输到SD卡,那么您不能这样做。
但你可以用一种方法。您将图像放在服务器上,在第一次打开应用程序时,您可以下载图像并将其保存在SD卡中,然后从SD卡进行访问。我有一个类似的要求-在应用程序中包含一组图像,但在我的情况下,任何用户或应用程序都必须能够访问图像,而不仅仅是解压缩图像的应用程序。我将它们存储在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);
}
}
private void loadCopyResources(){
//将资源复制到任何活动都可以使用的空间
字符串源名称;
字符串资源名;
字符串文件名;
智力资源;
字符串typeName=sourceSink.Types.photo.toString();
对于(sourceSink.Sources源:sourceSink.Sources.values()){
对于(int i=0;i
我没有包括sourceSink,它只是我需要复制的文件名和文件类型的列表 这不会使应用程序变小。如果你有500个图像,每个200kb,你的应用程序仍然会占用100MB的内存,还需要空闲空间来安装。