I would like to log raw data like arrays in my logcat, so I know what is the output. Lets say I have an array... like that:
File[] mp3List = ...
Log.v("test", mp3List);
Why can't I just log the array to console? How can I do it?
You can't log the array because it is just an Object. LogCat has no idea how to deal with it or display it in a way you want.
If each File object has a toString() method that displays the information that you want you can use:
Log.v(Arrays.toString(mp3List));
Otherwise you'll have to concatenate your own String to log:
StringBuilder sb = new StringBuilder();
for(File f : mp3List) {
sb.append(f.getName());
}
Log.v(sb.toString());
The reason why this doesn't work is simply because the 2nd argument of Log.v
is a String
not a File[]
. Java strictly enforces argument types.
Update:
You can easily transform the information contained a File
object into a String
object. All java objects implement a toString()
, which if I remember correctly returns a combination of the ClassName
and the address
of the object is located. However this usually doesn't contain useful information. So you need to define the conversion yourself.
Now to convert from File[]
to String
is more complicated because you when call a method on an array it works on the array object rather than on members of an array (which contains the information I suspect you care about). So calling mp3List.toString()
will return a single string that describes the array object and not the information contained in the array.
So you'll probably want to write a method like this:
String fileArrayToString(File[] f){
String output = "";
String delimiter = "\n" // Can be new line \n tab \t etc...
for (int i=0; i<f.length; i++)
{
output = output + f[i].getPath() + delimiter;
}
return output;
}
And then call make your log call as follows:
Log.v("MyTag", fileArraytoString(mp3List);
However this might be hard to read.
I personally would do it like this:
for (int i=0; i<mp3List.legnth; i++)
Log.v("MyTag", Integer.toString(i) + ":" + mp3List[i].getPath());
Its simpler, produces cleaner log messages and is easier to understand what is going on as a programmer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With