Phil
Phil

Reputation: 583

java android new String

I have to convert a byte array to a string. Therefore I use this function:

public static String byteToString(byte[] bytes) {
    String str = new String(bytes, Charset.forName("UTF8"));

    return str;
}

The problem is, that it requires API 9:

Call requires API level 9 (current min is 8): new java.lang.String

but I want to provide my app to API 8 users too. Is there any possibility or alternative?

Upvotes: 3

Views: 948

Answers (2)

Blackbelt
Blackbelt

Reputation: 157437

You can't. What you can do is to check for the sdk version

  if (Build.VERSION.SDK_INT >= 9) {
    // Constructor with charset
  } else {
   // Constructor without charset, providing the charset as string
  }

Froyo devices are something like 0.7%

Upvotes: 3

fabian
fabian

Reputation: 82461

You can use new String(bytes, "UTF8") (The constructor String(byte[], String)). It's always available (API level 1+).

Upvotes: 8

Related Questions