将 Bitmap 对象转换为存在问题的 Base64 字符串

发布于 2024-12-10 16:14:11 字数 1070 浏览 0 评论 0原文

我有一个简单的应用程序,其中从相机抓取图像,然后传递给我的 onActivityResult() 方法。但是,我无法将位图对象编码为 Base64 字符串。 Eclipes 告诉我,行 byte[] returned Image = Base64.encode(b, Base64.DEFAULT); 应该是 byte[] 而不是 String,所以这就是我认为问题所在(因此它下面的行试图强制它作为字符串对象)。我的代码如下,该方法被触发并出现日志,但数据不是base64。

任何人都可以帮助我吗?

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    switch(requestCode){
        case TAKE_PHOTO_CODE:
            if( resultCode == RESULT_OK ){
                Bitmap thumbnail = (Bitmap) intent.getExtras().get("data");
                ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
                byte[] b = baos.toByteArray(); 
                byte[] encodedImage = Base64.encode(b, Base64.DEFAULT);
                String encodedImageStr = encodedImage.toString();

                Log.e("LOOK", encodedImageStr);


            }
            // RESULT_CANCELED
        break;          
    }               
}

I have a simple application where a image is grabbed from the camera, then passed to my onActivityResult() method. I however can't encode the bitmap object into a base64 string. Eclipes tellsi me that the line byte[] encodedImage = Base64.encode(b, Base64.DEFAULT); should be a byte[] instead of a String, so this is where i think the issue is (hence the line below it trying to force it as a string object). My code is below, this method gets triggered and the Log appears, but the data is NOT base64.

Can anyone help me please.

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    switch(requestCode){
        case TAKE_PHOTO_CODE:
            if( resultCode == RESULT_OK ){
                Bitmap thumbnail = (Bitmap) intent.getExtras().get("data");
                ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
                byte[] b = baos.toByteArray(); 
                byte[] encodedImage = Base64.encode(b, Base64.DEFAULT);
                String encodedImageStr = encodedImage.toString();

                Log.e("LOOK", encodedImageStr);


            }
            // RESULT_CANCELED
        break;          
    }               
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软甜啾 2024-12-17 16:14:11

数组的内容执行任何操作

数组对象的 toString 不对您应该使用的

String encodedImageStr = new String(encodedImage);

,或者您可以直接转到 String

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

the toString of array object don't do anything with the contents of the array

you should use

String encodedImageStr = new String(encodedImage);

or you can go directly to String with

String encodedImageStr = Base64.encodeToString(b,Base64.DEFAULT);
轻拂→两袖风尘 2024-12-17 16:14:11
 Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                encode = Base64.encodeBytes(byteArray);
                try {
                    byte[] decode = Base64.decode(encode);
                    Bitmap bmp = BitmapFactory.decodeByteArray(decode, 0,
                            decode.length);
                    imgview_photo.setImageBitmap(bmp);
                                }
 Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                encode = Base64.encodeBytes(byteArray);
                try {
                    byte[] decode = Base64.decode(encode);
                    Bitmap bmp = BitmapFactory.decodeByteArray(decode, 0,
                            decode.length);
                    imgview_photo.setImageBitmap(bmp);
                                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文