Android:Base64 示例问题

发布于 2024-11-30 04:51:47 字数 474 浏览 1 评论 0原文

目前我正在使用 kSoap 将数据发布到 C# Web 服务。现在我需要使用 Base64Binary 从我的机器上传图像。我搜索了互联网上的所有内容,但无法找到合适的解决方案。

有一个解决方案 外部 Base64 类 示例,但我对本机解决方案感兴趣,因为 有一个 API来自安卓2.2.

由于我是新手,我自己无法做到。基本上我有一个图像的SD卡文件路径,我想将它们转换成Base64格式来上传。

希望有人可以帮助我或指导我找到正确的文件。

提前致谢。

currently I'm using kSoap to publish data to C# web services. Now I've come to the part where I need to upload images from my machine using Base64Binary. I searched everywehre internet but couldn't come up with a proper solution.

there is a solution with external Base64 class example but I'm interested in native solution as there is a API from Android 2.2.

Since I'm a newbie I couldn't do it myself. Basically I have a sd card file path of images, I want to convert them into Base64 format to upload.

Hope someone can help me or direct me to proper documents.

Thanks in advance.

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

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

发布评论

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

评论(2

谁把谁当真 2024-12-07 04:51:47

请尝试这个,使用您指定的链接中的 Base64.java 。

Bitmap bmImage = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + File.separator +  "Your filename");
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
bmImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);   
String encodedString = Base64.encodeBytes( baos.toByteArray() );

您应该根据您的文件类型更改此 Bitmap.compressFormat.JPEG 语句的扩展名。您可以使用以下代码将 Base 64 字符串解码为图像

byte[] b;
b = Base64.decode("base 64 string");
final Bitmap unscaledBitmap = BitmapFactory.decodeByteArray(b, 0, b.length );  

Please try this, use Base64.java from link you have specified.

Bitmap bmImage = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + File.separator +  "Your filename");
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
bmImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);   
String encodedString = Base64.encodeBytes( baos.toByteArray() );

You should change extension of this Bitmap.CompressFormat.JPEG statement according to your file type. You can decode a base 64 string to image using following code

byte[] b;
b = Base64.decode("base 64 string");
final Bitmap unscaledBitmap = BitmapFactory.decodeByteArray(b, 0, b.length );  
夏了南城 2024-12-07 04:51:47

我明白了这个问题

String Image64 = null;


        try {
            Image64 = Base64.encodeToString((getBytesFromFile(new File(path))), DEFAULT);

        } catch (IOException e) {
            e.printStackTrace();

        }

//encording
    public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);

        // Get the size of the file
        long length = file.length();

        // You cannot create an array using a long type.
        // It needs to be an int type.
        // Before converting to an int type, check
        // to ensure that file is not larger than Integer.MAX_VALUE.
        if (length > Integer.MAX_VALUE) {
            // File is too large
        }

        // Create the byte array to hold the data
        byte[] bytes = new byte[(int)length];

        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }

        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }

        // Close the input stream and return bytes
        is.close();
        return bytes;
    }

I figured out the issue

String Image64 = null;


        try {
            Image64 = Base64.encodeToString((getBytesFromFile(new File(path))), DEFAULT);

        } catch (IOException e) {
            e.printStackTrace();

        }

//encording
    public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);

        // Get the size of the file
        long length = file.length();

        // You cannot create an array using a long type.
        // It needs to be an int type.
        // Before converting to an int type, check
        // to ensure that file is not larger than Integer.MAX_VALUE.
        if (length > Integer.MAX_VALUE) {
            // File is too large
        }

        // Create the byte array to hold the data
        byte[] bytes = new byte[(int)length];

        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }

        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }

        // Close the input stream and return bytes
        is.close();
        return bytes;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文