FATAL EXCEPTION: Animation Thread.... help with optimizing images when running in android mode?
in
Android Processing
•
1 year ago
FATAL EXCEPTION: Animation Thread
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:525)
at processing.core.PApplet.loadImage(PApplet.java:3590)
at processing.core.PApplet.loadImage(PApplet.java:3576)
at processing.test.dogsmeow.dogsMeow.loadImg(dogsMeow.java:121)
at processing.test.dogsmeow.dogsMeow.setup(dogsMeow.java:61)
at processing.core.PApplet.handleDraw(PApplet.java:1855)
at processing.core.PGraphicsAndroid2D.requestDraw(PGraphicsAndroid2D.java:161)
at processing.core.PApplet.run(PApplet.java:1746)
at java.lang.Thread.run(Thread.java:1019)
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:525)
at processing.core.PApplet.loadImage(PApplet.java:3590)
at processing.core.PApplet.loadImage(PApplet.java:3576)
at processing.test.dogsmeow.dogsMeow.loadImg(dogsMeow.java:121)
at processing.test.dogsmeow.dogsMeow.setup(dogsMeow.java:61)
at processing.core.PApplet.handleDraw(PApplet.java:1855)
at processing.core.PGraphicsAndroid2D.requestDraw(PGraphicsAndroid2D.java:161)
at processing.core.PApplet.run(PApplet.java:1746)
at java.lang.Thread.run(Thread.java:1019)
when I run my sketch on the emulator [havnt tried a device yet] the above error is recieved right at 21 out of 26 images that are loaded.what the sketch does is load 26 images and 26 sounds and lets you cycle through them. if you click on an image it plays a sound that is associated with that image. all of them from both sets are kilobytes in size. the largets image is 30 kb.
Ive been reading and i understand that when in memory the images are height*width*4 or something similar. All the images are 600x600 pixels
my questions.
1. is that large?
2. how do I use smaller images and scale them up in processing and would that help?
3. is there something happening Im not aware of?
ive tried amping up the accessable memory for the emulator but no matter what I get this error.
any help would be great.
code:
- import ddf.minim.*;
import ddf.minim.signals.*;//dont need yet
import ddf.minim.analysis.*;//dont need yet
import ddf.minim.effects.*;//dont need yet
//our main array var's
int i;//image
int j;//sound
//name our arrays
String[] imgUrls;
String[] sndUrls;
//array objects
PImage[] images;
AudioSnippet[] sounds;- //our left and right arrows
PImage right_arrow;
PImage left_arrow;
//make our color for hover-over global
color l;
color r;
//minim
Minim minim; - void setup(){
size(800, 600);
background(0);
loadImg();//load images
loadSnd();//load sounds
r = color(0, 0, 0, 200);//set starting value
l = color(0, 0, 0, 200);//set starting value
}//setup
void draw(){
drawRecR();//draw button image
drawRecL();
noLoop(); //stop the images array from loading over and over
image(images[i], 100, 0 );//draws our current image [i]
}//draw
void mousePressed(){
//if the mouse is over left 'button'
if (mouseX >= 0 && mouseX <= 100){
println("left button");
i--;
j--;
if ( i < 0){
//must subtract from array length to
//account for 0 being counted
i = images.length - 1;
j = sounds.length - 1;
}//if
}//ifML
//if the mouse is over the center area/image
else if (mouseX >= 101 && mouseX <= 699){
println("center button");- //play the sound when clicked
play();
}//ifMC
//if the mouse is over the right button
else if (mouseX >= 700 && mouseX <= 800){
println("right button");
i++;
j++;
if ( i >= (imgUrls.length)){
i = 0;
j = 0;
}//if
}//ifML
println(i);//what is our image #
println(j);//what is our sound #
println(mouseX);//where is our mouse
//must use this because 'noLoop()' was - //causes draw() to run one time
redraw();
}//mouse pressed
//push list of images into array
void loadImg(){
imgUrls= loadStrings("listI.txt");
images = new PImage[imgUrls.length];
for(int i = 0; i<imgUrls.length;i++){
images[i] = loadImage(imgUrls[i]);
println(i);
}//fori
}//loadImg- //push list of sounds into array
void loadSnd(){
sndUrls= loadStrings("listS.txt");
sounds = new AudioSnippet[sndUrls.length];
for(int j = 0; j<sndUrls.length;j++){
minim = new Minim(this);
sounds[j] = minim.loadSnippet(sndUrls[j]);
println(j);
}//fori
}//loadSnd - ////loads and positions image for 'button'
void drawRecL(){
left_arrow = loadImage("arrowLeft.jpg");
image(left_arrow, 0,0);
fill(l);
rect(0,0,100,600);
}//drawRec - //loads and positions image for 'button'
void drawRecR(){
right_arrow = loadImage("arrowRight.jpg");
image(right_arrow, 700,0);
fill(r);
rect(700,0,100,600);
}//drawRec - //our hover over affect is just a black rectangle at
//a lower alpha that is hidden if the mouse is over it
//because noLoop was called 'mouseMoved' was the - //only way found to get this affect as it continuously tracks the mouse
void mouseMoved(){
//if the mouse is over left button
if (mouseX >= 0 && mouseX <= 100){
l = color(0, 0, 0, 10);
}else if (mouseX >= 101 && mouseX <= 699){
l = color(0,0,0,200);
}//if
//if mouse is over right button
if (mouseX >= 700 && mouseX <= 800){
r = color(0, 0, 0, 10);
}else if (mouseX >= 101 && mouseX <= 699){
r = color(0,0,0,200);
}//if
loop();
}//rhover
//control when the sound file is played
void play(){
//if it is not playing then go ahead and play
if (!sounds[j].isPlaying()){
// makes sure the file plays from start
sounds[j].rewind();
sounds[j].play();
}//if
}//play
//when the program is closed, close all the sound threads
void close(){
sounds[j].close();
minim.stop();
//
super.stop();
}//stop
1