Many image files in res/drawable, How to access in Android?
I have to make a dedicated image viewer app for Android 2.x.
There are too many jpeg image files: about 2000~ jpegs, over 100MB.
I want access the image files with their file names, but I couldn't find such an example.
By the way, is it okay to put many image files in /res/drawable folder?
I heard that the android application cannot be installed on sdcard and
the program repo开发者_C百科sitory is very small so 100MB app cannot be installed generally.
I found some examples which download the large data files on sdcard online,
but I cannot run a web server to host the data files,
and I must upload the fully packaged program on Android Market. (Should I build one apk file?)
What are the best practices for managing too many resource images (or something) in Android?
I think you are going to have a hard time convincing users to install a program that is 100 MB into the internal memory of their phones. It would be much better to sideload the images onto the SD card. There are a number of fairly cheap file hosting services available such as Amazon S3.
Also, you should consider allowing the users to download the images in small groups instead of in one large chunk.
The G1 has 256MB of internal storage for applications. Even on the Nexus One there's only 512MB so I think it's unlikely that anyone would want a single application taking up such a high proportion of this storage, so creating a 100MB+ .apk file isn't going to be practical.
You are right that stock android phones cannot run applications from the SD Card. (There are custom firmwares that allow this, but this isn't going to help you as only a small minority of users run these.)
You say that you cannot run a webserver, but unfortunately, I think that's your only real option here. You could dowload the images as needed and cache them on the SD Card if you had them on a webserver somewhere. Configuring a webserver to serve a whole of images is pretty straightforward, although you may need to do some work to stop people looking at the images using a web browser rather than your app if you're charging for it.
ImageView iv = new ImageView(context);
iv.setImageResource(R.drawable.icon);
To access a large array of images sitting in a directory on the SD card:
ImageView iv= ...
int imageIndex=0; //can access all image files by imageIndex, 0=first entry
List<String> ImageList=FindImages();
if(ImageList!=null && ImageList.size()>=currentIndex){
iv.setImageDrawable(Drawable.createFromPath(ImageList.get(imageIndex)));
}
//put your image files on SD in DIRECTORY
private List<String> FindImages() {
final List<String> tFileList = new ArrayList<String>();
Resources resources = getResources();
// may use array of valid image file extensions
//String[] imageTypes = ...
FilenameFilter[] filter = new FilenameFilter[imageTypes.length];
int i = 0;
/* can use string array of image types:
for (final String type : imageTypes) {
filter[i] = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith("." + type);
}
};
i++;
}*/
filter[i] = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".png");
}
};
FileUtils fileUtils = new FileUtils();
File[] allMatchingFiles = fileUtils.listFilesAsArray(
new File(DIRECTORY), filter, -1);
for (File f : allMatchingFiles) {
tFileList.add(f.getAbsolutePath());
}
return tFileList;
}
Haven't tried it myself, but looks like this could work:
public BitmapDrawable(Resources res, String filepath);
https://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html#BitmapDrawable%28android.content.res.Resources,%20java.lang.String%29
精彩评论