A.T A.T - 2 months ago 6
Android Question

upload image to server and then it fetch in android

i have USERS table in phpmyadmin data base. in the table i have a column which holds link to the user profile picture. but i think the image i`m uploading its not good. when i open it i see just a little square like this:

enter image description here

EDIT: the picture part is fixed

and when i`m trying to fetch it with piccaso i get nothing.
the way i upload the picture is encoding it to string and then decode it in the register.php

this is the method which converting to image to string:

public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;

this is my php where im uploading the image:

$firstName = $_POST['firstname'];
$lastName = $_POST['lastname'];
$passWord = $_POST['password'];
$phoneNumber = $_POST['phonenumber'];
$apartmentNumber = $_POST['apartmentNumber'];
$image = $_POST['image']; //this is the encoded string

//upload image to image folder in the server
$upload_folder = "upload";
$path = "$upload_folder/$phoneNumber.jpeg";
if(file_put_contents($path, base64_encode($image)) != flase){
echo "uploaded";
$image_link = "my url/$path";
$result = mysqli_query($con,"INSERT INTO Users (firstName, lastname, password, phone , apartmentNum,image)
VALUES ('$firstName', '$lastName', '$EncryptPassword', '$phoneNumber', '$apartmentNumber' ,'$image_link')");

this is the php where i`m creating the json object:

$sql = ("select * from Users where phone = '$phone' and password = '$password' ");

$check = mysqli_fetch_array(mysqli_query($con,$sql));

$hasApartment = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($hasApartment);
$apartmentNumber = $row['apartmentNum'];
$firstName = $row['firstName'];
$lastName = $row['lastname'];
$image = $row['image'];
echo '{"query_result":"SUCCESS", "apartmentNumber":' . $apartmentNumber.' , "firstName":' . trim($firstName) .' ,"lastName":' . trim($lastName).', "image":' . $image.'}';


i get this exception:

org.json.JSONException: Unterminated object at character 102 of {"query_result":"SUCCESS", "apartmentNumber":54 , "firstName":a ,"lastName":a, "image":XXXXXX.XX.XX/upload/112.jpeg}

this is the json object:

{"query_result":"SUCCESS", "apartmentNumber":54 , "firstName":a ,"lastName":a, "image":XXXXXX.XX.XX/upload/112.jpeg}

char 102 its the "u" in upload

This is whers i do a GET request. In onResponse() i try to handle the json text as json object:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response) {
try {

String query_result = response.getString("query_result");
switch (query_result){
case "SUCCESS": // user name and password exists and mathces and user associated with apartment
int apartmentNum = Integer.parseInt(response.get("apartmentNumber").toString());

Intent intent = new Intent(LoginActivityScreen.this , HomeActivityScreen.class);

SharedPreferences.Editor editor = preferences.edit();

editor.putString("phoneNumber" , PhoneValidationActivity.PHONE_NUMBER);
editor.putString("firstName", response.getString("firstName"));
editor.putString("lastName", response.getString("firstName"));
editor.putBoolean("loggedIn", true);
editor.putInt("apartmentNumber", apartmentNum);

Bitmap profilePicture = Picasso.with(context).load("http://roomates.96.lt/upload/0547517307.jpeg").get();



case "FAILURE": //user name or password are incorrect
Toast.makeText(getApplicationContext(), "wrong name or password", Toast.LENGTH_SHORT).show();

} catch (JSONException e) { //this exception is caught
Log.e("e" , e.toString());
} catch (IOException e) {
Log.e("e", e.toString());
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {


String query_result = response.getString("query_result")

Do you see the diference between "query_result":"SUCCESS" and "image":XXXXXX.XX.XX/upload/112.jpeg ?