2017-07-21 90 views
0

变量使用onActivityResult方法和bitmapToBase64(Bitmap bitmap)方法变量内API不能是外访问保存base64编码图像划分成MySQL数据库
它存储空值在MySQL数据库通过URL在机器人而。如何在机器人

例如:在下面的代码中,我将编码图像base64String存储在编码器中。
但尝试从外部访问方法,并通过url传递编码的字符串。 它在数据库中保存为null

public String bitmapToBase64(Bitmap bitmap) { 
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, byteArrayOutputStream); 
byte[] byteArray = byteArrayOutputStream.toByteArray(); 
encoderes = Base64.encodeToString(byteArray, Base64.DEFAULT); 
encodedImage.setImgStr(encoderes); 
encodestr.setText(encoderes); 

return encoderes; } 

编码代码段

@Override 
    public void onActivityResult (int requestCode, int resultCode, Intent 
    data) 
    { 
    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) { 
    Uri imageUri = data.getData(); 
    imageView.setImageURI(imageUri); 
    imageView.buildDrawingCache(); 
    Bitmap bmap = imageView.getDrawingCache(); 
    bitmapToBase64(bmap);} else if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) { 
    Uri imageUri = data.getData(); 
    imageView.setImageURI(imageUri); 
    imageView.buildDrawingCache(); 
    Bitmap bmap = imageView.getDrawingCache(); 
    bitmapToBase64(bmap); 

    //Bitmap image = (Bitmap) data.getExtras().get("data"); 

} }                  
public String bitmapToBase64(Bitmap bitmap) { 
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, byteArrayOutputStream); 
byte[] byteArray = byteArrayOutputStream.toByteArray(); 
encoderes = Base64.encodeToString(byteArray, Base64.DEFAULT); 
encodedImage.setImgStr(encoderes); 
encodestr.setText(encoderes); 

return encoderes; } 
+0

为什么不把它保存为blob?与b64相比,尺寸和性能都没有增加。 – dedda1994

回答

0

使用此方法:

public static String convertToBase64(Bitmap bitmap) { 
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
     bitmap.setDensity(IMAGE_DENSITY); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream); 
     byte[] byteArray = byteArrayOutputStream.toByteArray(); 
     setDpiInfo(byteArray, IMAGE_DENSITY); 
     return Base64.encodeToString(byteArray, Base64.NO_WRAP); 
    } 
+0

它存储在MySQL数据库中的空值..? –

0

使用此

public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) { 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream); 
    byte[] byteFormat = stream.toByteArray(); 
    // get the base 64 string 
    String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP); 

    return imgString; 
} 

ù se returened value to send to server

+0

它在mysql数据库中存储空值..? –