Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve Base64.encodeToString

I am trying to save images on firebase using code below.

  Bitmap bm = BitmapFactory.decodeFile(imgDecodableString);
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
  byte[] byteArray =  baos.toByteArray();
  String encodedImage = Base64.encodeToString(byteArray,Base64.DEFAULT);

But i am facing an error :

cannot resolve method 'encodeToString(byte[],?)' 
cannot resolve symbol 'DEFAULT'

How to fix this error.

like image 446
Digvijay Singh Thakur Avatar asked May 07 '16 09:05

Digvijay Singh Thakur


People also ask

How to encode and decode Base64 strings?

Once the Base64 API is created, both encoding and decoding are quite simple: Moreover, the decode () method of the Base64 class returns the decoded string: Another option is using the static API of Base64 instead of creating an instance: 4.

What is Base64 in Java 8?

Base64 is a binary-to-text encoding scheme that represents binary data in a printable ASCII string format. In this article, you’ll learn how to Base64 encode any arbitrary text in Java using the Base64 API that was introduced in Java 8.

What is the difference between Base64 and Base64 int linelength?

Base64 (int lineLength) creates the Base64 API in a URL-unsafe mode and controls the length of the line (default is 76). Base64 (int lineLength, byte [] lineSeparator) creates the Base64 API by accepting an extra line separator, which by default is CRLF (“ ”).

What characters are used in base64 encoding?

The basic Base64.getEncoder() function provided by the Base64 API uses the standard Base64 alphabet that contains characters A-Z, a-z, 0-9, +, and /. Since + and / characters are not URL and filename safe, The RFC 4648 defines another variant of Base64 encoding whose output is URL and Filename safe.


Video Answer


2 Answers

I think you have import org.apache.commons.codec.binary.Base64 in your file, please re-check and instead try using import android.util.Base64. It will resolve your problem for cannot resolve method 'encodeToString(byte[],?)'.

like image 115
Mayank Kushwaha Avatar answered Sep 23 '22 22:09

Mayank Kushwaha


Try it:

val encodedImage = encodeToString(b, DEFAULT)
like image 20
Álysson Alexandre Avatar answered Sep 23 '22 22:09

Álysson Alexandre