找到你要的答案

Q:base64 string is not uploaded in server android?

Q:base64字符串不是Android上传服务器?

I need to send the image (from gallery) to server in the format of base64 string. I get the path of the image and converted it to base64 string. but at server the image is not showing fully. only 10% of the image is showing at server side.Please any one help me how to convert the image to base64 string.

code:

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

image size:460kb

我需要把图像(从画廊)服务器在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

answer1: 回答1:

try with this i hope it will work for you and send using httpPost method.

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

and server side.

Base64 base64=new Base64();
saveImage(base64.decode(profilePic),"yourName");

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方法。

公共静态String BitmapToString(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),"yourName");

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



 }
android  base64  android-image  android-gallery