tags:

views:

39

answers:

2

Here's a snip that successfully reads off, to Eclipse LogCat, the height and width of the .PNG on the SD card:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

//...

        Bitmap bmp = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/sample.png"); 
        int width = bmp.getWidth(); 
        int height = bmp.getHeight();
        int[] pix = new int[width * height]; 
        bmp.getPixels(pix, 0, width, 0, 0, width, height); 

        int R, G, B; 

        for (int y = 0; y < height; y++){
            for (int x = 0; x < width; x++) 
            {               
                int index = y * width + x;

                R = (pix[index] >> 16) & 0xff;     //bitwise shifting
                G = (pix[index] >> 8) & 0xff;
                B = pix[index] & 0xff;

    // [This is where I'd put it.  But I can't get a red line off the editor when I do anything.]  
            }
        }

        Log.v(TAG, width + "  " + height); 

My problem: I can't for the life of me use Log.v() to report the value of R or G or B. Everything I'm doing draws a red line under v or something when I try. I've tried toString() and everything. Help please. Thanks.

+1  A: 

You'd have to log within the loop in order to log each pixel. Was that where you were trying to log it?

If you were trying to log it at the same place as the height and width, which pixel's value were you expecting to log? What if the image was empty? If this is what you were trying to do, the error would be due to the use of uninitialized variables... but that error just leads to a deeper question of exactly what you're trying to do.

EDIT: Okay, so it turns out it was slightly different - this is a good example of why it's important to give the code which is failing as well as the code which isn't, and say what the error message is.

So you were trying to call a method with this signature:

void v(String, String)

But you were trying to call it like this:

Log.v(TAG, R);

which is trying to pass an int as a String. You need to convert the int value to a string. IMO, the best way to do this is with String.valueOf:

Log.v(TAG, String.valueOf(R));

Having said that, if you do any string concatentation, the compiler will add the conversion for you. So you could do either of these:

Log.v(TAG, R + " " + G + " " + B);
Log.v(TAG, "Red: " + R);

As a side note, I would strongly suggest that if this is giving you a hard time, it would be worth reading a good introductory Java book - one which doesn't focus on Android - and try writing small Java console apps for a while. It's hard enough learning how to develop on a new client platform to start with, without trying to learn a language at the same time. By putting in a bit more time on the language up-front, you're likely to save effort in the long run.

Jon Skeet
I've since added where I'd put the Log.v, if it worked. The question here is: nothing I do there makes R or G or B make it out of the Eclipse editor without a redline under something on the statement. I don't know what to write. Try this if you can on a HelloAndroid with this snip added (and the approriate imports), because I can't get a statement to report TGB values out the door. Thanks for any assistance.
@user225626: It should be fine there. Please show the exact line you tried to use *and* the error message that Eclipse showed you.
Jon Skeet
'RGB', not 'TGB'. Now the comment editor here is getting in my way. (Won't allow me to edit after a few minutes.)
@user225626: I don't have any android development environment installed, but I suspect it's quite a simple problem... but we *do* need to see the line that you've tried, and the error that it was giving. Otherwise we'd just be guessing.
Jon Skeet
I think my problem is I don't know the syntax well enough to express in a statement R (or G or B). If I write this: Log.v(TAG, R); then I get an Eclipse editor redline under v. But I know of no other statement to write.
All right: "The method v(String, String) in the type Log is not applicable for the arguments (String, int)" How do I write something that satisfies this requirement?
I've tried writing it this way: Log.v(TAG, toString(R)); but Eclipse just puts a red line under "toString"
@user225626: Where do you expect the `toString(int)` method to come from? Try either `Log.v(TAG, R + " " + G + " " + B);` to log them all together, or `Log.v(TAG, String.valueOf(R));` to log a single value. Will edit my answer.
Jon Skeet
Oh. solved: java.lang.String has a valueOf method. (I'm new to Java too.) Thanks Jon.
I never knew that side benefit compiler feature existed with concatenation. Thanks for that too.
A: 

Oh. solved: java.lang.String has a valueOf method. (I'm new to Java too.) Thanks Jon.