如何将字符串路径转换为Base64字符串?

How to convert string path to Base64 string?

简单来说:在我的 onActivityResult() 方法中,我使用图像的路径将图像上传到图像视图中。它又快又好,但问题是,我想将该字符串路径转换为 ​​Base64 字符串。我想将图像显示为 Base64 字符串并将 Base64 字符串发送到我的数据库,以便它可以在我们应用程序的 IOS 版本中使用。下面是我目前拥有的,我已经尝试过位图和其他东西,但我总是遇到内存不足的错误!我希望你们能告诉我如何将图像路径转换为 ​​base64 字符串并将其上传到 imageviews 而不会出现内存不足错误。

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        addImageImageView.setVisibility(View.GONE);
        if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data != null) {
            //First we gotta make sure to add the images to
            ArrayList<Image> imagesFromGallery = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);//Image is a personal object I made.

            for (int i = 0; i < imagesFromGallery.size(); i++) {
                images.add(imagesFromGallery.get(i).path);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp.setMargins(20, 20, 0, 0);//5dp = 20 pixels
                lp.height = 720;//180dp = 720pixels
                lp.width = 1400;//330dp = 1320 pixels.
                ImageView newImageView = new ImageView(this);
                newImageView.setLayoutParams(lp);
                Glide.with(this).load(imagesFromGallery.get(i)).centerCrop().into(newImageView);
                imageLinearLayout.addView(newImageView, 0);
            }
    }

发送端(编码)

byte[] data = imagePath.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);

接收方(解码)

byte[] data = Base64.decode(base64, Base64.DEFAULT);
String imagePath = new String(data, "UTF-8");