Reputation: 792
I'm trying run the following code, however I cannot use encodeBase64()
.
I've already tried to import such libraries using Alt + Enter.
How can I make it work?
Upvotes: 6
Views: 8687
Reputation: 1
Ehy man Base64.encodeBase64 is not an Android SDK resource so you are not gonna be able to use it but you can actually use android.util.Base64
import android.util.Base64;
// Encoding
String originalData = "Hello, world!";
byte[] encodedBytes = Base64.encode(originalData.getBytes(),
Base64.DEFAULT);
String encodedString = new String(encodedBytes,
Charset.defaultCharset());
// Decoding
byte[] decodedBytes = Base64.decode(encodedBytes, Base64.DEFAULT);
String decodedString = new String(decodedBytes,
Charset.defaultCharset());
You can use this code and it shoud work perfectly, in case you can check the documentation online: https://developer.android.com/reference/android/util/Base64
Upvotes: 0
Reputation: 4745
Base64.encodeBase64()
public static byte[] encodeBase64(final byte[] binaryData)
You are using https://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html, not https://developer.android.com/reference/android/util/Base64
You could add the following dependency to make build pass like this: take Gradle for example.
// import org.apache.commons.codec.binary.Base64;
// https://mvnrepository.com/artifact/commons-codec/commons-codec
implementation group: 'commons-codec', name: 'commons-codec', version: '1.10'
But I suggest you to use Android version since you are developing Android application since you are using Android Studio.
String data;
Base64.encodeToString(data.getByte("UTF-8"), Base64.DEFAULT);
public static String encodeToString(byte[] input, int flags);
Upvotes: 0
Reputation: 3785
use this:
String result = Base64.encodeToString(data, Base64.DEFAULT);
instead of what you are using, i also advise you to use:
.getBytes("UTF-8");
instead of data.getBytes();
UTF-8 is always a better choice.
hope this will help you.
Upvotes: 10