簡體   English   中英

如何在Android中將字符串圖像路徑轉換為Base64字符串?

[英]How to convert a string image path to Base64 string in android?

所以即時通訊與將字符串路徑上傳到ima​​geViews一起工作,並且工作正常,但是我的IOS應用程序無法使用我的字符串路徑,因此我需要切換到基本64個字符串。 如何將我的字符串路徑轉換為基本的64位圖像,然后將其上傳到ima​​geview,而不會出現內存不足錯誤?

以下是我目前在myActivityResult()上擁有的內容。

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

您可以使用Base64 Android類:

String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

您必須先將圖像轉換為字節數組。 這是一個例子:

Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
byte[] b = baos.toByteArray();

所以byteArrayImage是b。

String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM