Take picture and convert to Base64

后端 未结 4 1139
陌清茗
陌清茗 2020-12-14 02:24

I use code below to make a picture with camera. Instead of saving I would like to encode it to Base64 and after that pass it to another API as an input. I can\'

4条回答
  •  长情又很酷
    2020-12-14 02:44

    If you want your base64 String to follow the standard format, add this after getting your base64 method from any of the provided answers

    String base64 =""; //Your encoded string
    base64 = "data:image/"+getMimeType(context,profileUri)+";base64,"+base64;
    

    The method to get imageExtension is

       public static String getMimeType(Context context, Uri uri) {
            String extension;
            if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
                //If scheme is a content
                final MimeTypeMap mime = MimeTypeMap.getSingleton();
                extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
            } else {
                //If scheme is a File
                //This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters.
                extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());
            }
            return extension;
        }
    

提交回复
热议问题