Question

I'm developing a live wallpaper that allows the user to choose a static image from their Gallery and make that the background on the homescreen. I was following the accepted answer from here: Choosing background for Live Wallpaper and everything worked up until I had to implement the following code (the last portion in the answer):

void getBackground() { 
if (this.cvwidth == 0 || this.cvheight == 0 || this.visibleWidth == 0) {
this.cvwidth = 480;
this.cvheight = 854;
this.visibleWidth = 480;}
if(new File(imageBg).exists()) {
int SampleSize = 1;
do {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bg = BitmapFactory.decodeFile(imageBg, options);
SampleSize = (int) (Math.ceil(options.outWidth/(this.visibleWidth * 2))*2);
options.inJustDecodeBounds = false;
try {options.inSampleSize = SampleSize;
bg = BitmapFactory.decodeFile(imageBg, options);}
catch (OutOfMemoryError e) {
SampleSize = SampleSize * 2;
}
} while (bg == null);

bg = Bitmap.createScaledBitmap(bg, this.cvwidth/2, this.cvheight, true);}
else {bg = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
bg = Bitmap.createScaledBitmap(bg, this.cvwidth/2, this.cvheight, true);}
LoadText = "";
} 

Everything else worked pretty much as-is after adding in the appropriate variables. What is confusing me here is the line else {bg = BitmapFactory.decodeResource(getResources(), R.drawable.bg);, which gives me the error bg cannot be resolved or is not a field, referring to R.drawable.bg. What am I missing here?

Anyone?

Was it helpful?

Solution

That was my original (long winded answer) many years ago and I'm foggy about the whole code now as I don't develop for android at all now.

However, if my memory serves me well, you need an image saved in your drawables folder. I called mine 'bg'. It was used as the default image when the app started and was also used as a roll back image if the code couldn't resolve an image that the user chose (e.g. it was deleted later).

I hope I'm correct, but it was about three years ago I wrote that answer.

Good luck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top