1

So I am making an app that has over 200 sunset pictures (taken by myself:P). I want to display them all via android.R.layout.simple_gallery_item I tried tossing them all in the drawable folder, then bitmapping each one and drawing it on the screen, but I get an Out of memory error.

What would be the best way to store these jpg?

The only two ways I can think of are:

  • A separate zip file download from the app?
  • Drawable File? (Using bitmap wouldn't be the right way?)
  • Somehow storing them in the apk then pushing them onto the device before installing the app?(If possible, because if I am correct(which I am probably not), when building the apk, everything comes together and some files morph into one)

(PS: If can't tell, I am new to Android Developing)

Thanks!

Code:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    System.out.println("Noobs");

    ArrayList<Image> img = new ArrayList<Image>();
    for (int i=1; i<3; i++) {
        int imageResource = getResources().getIdentifier("@drawable/bm" + String.valueOf(i), null, getPackageName());
        img.add(ImageView.setImageResource(imageResource));
        System.out.println("Did Pic "+String.valueOf(i));
    }


    System.out.println("Come so far!");
    ListAdapter picAdapt = new ArrayAdapter<Image>(this, android.R.layout.simple_gallery_item, img);
    ListView picListView = (ListView) findViewById(R.id.gallery);
    picListView.setAdapter(picAdapt);

} }

Pythogen
  • 591
  • 5
  • 25
  • 1
    Store images in server and load using image loader libraries. (or) Convert all images from jpeg to webp, so size of the images can reduce – Harsha Vardhan Mar 25 '16 at 01:26
  • Would having the server cost me a monthly fee? Which one would you recommend? – Pythogen Mar 25 '16 at 01:43
  • 1
    Depends upon the size of the images..If image size is in kb, prefer bundle in the app. If size is more, store it in server (Use parse.com, but u can use until Jan, 17, If u have an account already there) Or some cloud servers which can help u – Harsha Vardhan Mar 25 '16 at 01:51
  • Alright, also if I choose to bundle it in app, how should I do so? Dropping it into the drawable folder and Bitmapping is too hard on the phone, is there someway I can just load it directly into an Image Array? Here is my code: http://pastebin.com/BsiUDVep I'm using a for loop to grab all the images from the drawable folder via uri and then trying to draw them up on the screen. The code gets stuck at the first image and declares out of memory. – Pythogen Mar 25 '16 at 02:05
  • 1
    If you have drawable already with you, then why bitmapping is required? You said about stuck @ first image, But there is no such code, u posted – Harsha Vardhan Mar 25 '16 at 02:12
  • 1
    If you have id with you, then why are you getting drawable and storing into array...rather than use setImageResource and pass id to the method – Harsha Vardhan Mar 25 '16 at 02:15
  • Could you post an example in the answer section of using the image id and setImageResource along with android.R.layout.simple_gallery_item? Thanks! Much appreciated:) – Pythogen Mar 25 '16 at 02:30

2 Answers2

4

I recommend you use picasso http://square.github.io/picasso/ or glide https://github.com/bumptech/glide
those two help you to get and optimize your images

Jerassi Ferrer
  • 284
  • 2
  • 8
  • Would either of these charge me a fee for using their servers? – Pythogen Mar 25 '16 at 03:36
  • Thanks! Vote up for you! I will select your answer if Harsha Vardhan's doesn't work out, I would rather keep it all in the app. – Pythogen Mar 25 '16 at 03:49
  • I looked further into the Picasso, it seems that it takes a URL and downloads the image, where would I host my pictures at that would do it for free? – Pythogen Mar 25 '16 at 15:44
  • 1
    Well you can use http://photobucket.com/desktop but also u can use google drive or dropbox and just generate share link – Jerassi Ferrer Mar 25 '16 at 18:23
0
    ArrayList<Integer> img = new ArrayList<Integer>();
    for (int i=1; i<3; i++) {
        int imageResource = getResources().getIdentifier(mContext.getPackageName() + ":drawable/bm" + String.valueOf(i), null, null);
        img.add(imageResource);

    }

    ImageView imageView = findViewById(R.id.<UR_ID>);        
    imageView.setImageResource(img.get(0));
Harsha Vardhan
  • 3,324
  • 2
  • 17
  • 22