Domanda

I'm writing an android app that contains about 500 images . there are somethings that make me worry, I don't want to use internet.

1-the application size will be very big , is there anyway to moving images to sd card while installing? some devices may don't have this amount of space on the phone .

2-should I make 3 images for hdpi , ldpi and mdpi ?

È stato utile?

Soluzione 2

  1. Yes, it will be big. No, you can't remove them from your package.
  2. No, you can make only hdpi images. Android will scale them automatically (which may slow down a bit the app).

Suggestion - use internet. Since the user has internet to download your app, he can wait to download the resources on first start. Also it give you the ability to add/remove files via online configuration. Just imagine if you have to add 1 image and upload new version - this means that the user will have to download the same huge package again.

Altri suggerimenti

You can put you image in asset folder. If you want to transfer image from assets to SD Card then you can't do like this. But you can do by one way. You put your image on server and at 1st time when you will open app you can download it and save it in SD Card and then access from there.

I had a similar requirement - include a bunch of images in the app, but in my case, the image had to be accessible by any user or app, not just the app that unpacked them. I stored them in the res/raw folder and copied them to user space on start up:

    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, which I didn't include, is just a list of file names and file types I needed copied.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top