【问题标题】:base64 string is not uploaded in server android?base64 字符串未上传到服务器 android 中?
【发布时间】:2015-05-02 09:02:27
【问题描述】:

我需要以 base64 字符串的格式将图像(来自画廊)发送到服务器。我得到图像的路径并将其转换为 base64 字符串。但在服务器上,图像没有完全显示。只有 10% 的图像显示在服务器端。请任何人帮助我如何将图像转换为 base64 字符串。

代码:

filePath = cursor.getString(columnIndex);    

Bitmap bitmapOrg = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(CompressFormat.PNG, 100, bao);
byte [] ba = bao.toByteArray();
ba1 =Base64.encodeToString(ba, Base64.DEFAULT);

图像大小:460kb

【问题讨论】:

    标签: android base64 android-image android-gallery


    【解决方案1】:

    试试这个,我希望它对你有用并使用 httpPost 方法发送。

    public static String BitmapToString(Bitmap bmp) {

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    String base64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
    return base64;
    }  
    

    和服务器端。

    Base64 base64=new Base64();
    saveImage(base64.decode(profilePic),"你的名字");

    public void saveImage(byte[] bytes,String name){
    
        String path="D:/"+name.hashCode()+".png";
     try {
            FileOutputStream f = new FileOutputStream(path);
            f.write(bytes);
            f.close();
        } catch (Exception e) {
    
        e.printStackTrace();
        }
    
    
    
     }
    

    【讨论】:

    • 使用httpPost方式发送。
    • @vikas ..我不知道服务器端代码(.net 服务)。我只是发送代码并在服务器端检查,而不用 http post 发送。我应该用 http post 发送吗?
    • @androidandroid 使用 httpPost 客户端 b,因为 httpGet 有限制,所以你不能发送大数据。 ex- HttpClient httpclient = new DefaultHttpClient(); HttpPost post=new HttpPost("your-Url");
    • 获取文件路径后,我将其转换为base64string并获取代码。之后我将代码发送给服务器人员并检查它....不使用服务
    猜你喜欢
    • 1970-01-01
    • 2021-11-03
    • 2016-06-10
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    相关资源
    最近更新 更多