简体   繁体   中英

I can't LogCat this value. Why?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM