【问题标题】:How to convert string path to Base64 string?如何将字符串路径转换为 ​​Base64 字符串?
【发布时间】:2016-10-28 23:13:30
【问题描述】:

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

 @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);
            }
    }

【问题讨论】:

标签: android base64 android-imageview android-image android-bitmap


【解决方案1】:

发送方(编码)

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");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 2014-02-15
    相关资源
    最近更新 更多