Sending blob data in json file to android app
I am trying to send image to json file to android app, here is my code:
public function allUserCarsAction()
{
$request = Request::createFromGlobals();
$UId = $request->request->get('UId');
$em = $this -> getDoctrine();
$id = $em -> getRepository("OBCarsTest2Bundle:user") ->findOneById($UId);
$UserCars = $em -> getRepository("OBCarsTest2Bundle:cars") ->findByidUser($id);
$a = 0;
if($UserCars != Null)
{
foreach($UserCars as $car)
{
$ModelId = $em -> getRepository("OBCarsTest2Bundle:models")->findOneById($car->getModels());
$BrandsId = $em -> getRepository("OBCarsTest2Bundle:brands")->findOneById($car->getBrands());
if($ModelId!=Null && $BrandsId != Null)
{
$infoCars = array ('name'=>$BrandsId->getCarBrand(),
'model'=>$ModelId->getCarModel(),
'carvin'=>$car->getVin(),
'price'=>$car->getPrice(),
'currency'=>$car->getCurrency(),
'pic'=>$pic=($car->getPic()));
$userCar[$a] = $infoCars;
$a++;
}
}
$Cars = array ('cars'=>$userCar);
$CarsSent = json_encode($Cars);
}
else
$CarsSent = '{"CarsSent":0}';
return new Response($CarsSent);
}
which is the object in which the image is configured:
/**
* @var blob
*
* @ORM\Column(name="$pic", type="blob")
*/
private $pic;
/**
* Set pic
*
* @param string $pic
* @return cars
*/
public function setPic($pic)
{
$this->pic = $pic;
return $this;
}
/**
* Get pic
*
* @return string
*/
public function getPic()
{
return $this->pic;
}
but my partner has this message when he tries to get a response:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>An Error Occurred: Internal Server Error</title>
</head>
<body>
<h1>Oops! An Error Occurred</h1>
<h2>The server returned a "500 Internal Server Error".</h2>
<div>
Something is broken. Please let us know what you were doing when this error occurred.
We will fix it as soon as possible. Sorry for any inconvenience caused.
</div>
</body>
</html>
when I put: "pic" => $ pic = ($ car-> getPic ())); in the comment, he gets all cars without images.
How can I send an image to a json file?
PS I spent 3 days trying to solve this but got nothing. I tried using this one http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html but didn't work.
source to share
You will need to encode it into base64
.
Considering what $car->getPic()
contains the actual binary data, you will need:
$pic=$car->getPic();
$infoCars = array (
'name'=>$BrandsId->getCarBrand(),
'model'=>$ModelId->getCarModel(),
'carvin'=>$car->getVin(),
'price'=>$car->getPrice(),
'currency'=>$car->getCurrency(),
'pic'=> base64_encode($pic) // <-- THIS
);
Depending on the purpose of the picture, the other side will have to be decoded. I know that CSS allows setting background-image
to base64
encoded value, however I am not familiar with the possibilities Android
.
Hope it helps ...
source to share