0

I have an application and I need to receive images from my database (before someone asks, yes it needs to be from a database).
In my PHP file I send the full complete string, but in Android I only receive half of the string or so.
Do you guys have any tip on why is this happening?
Can anyone help me?

Code

ServerRequest.java:

public void FetchServicoFotoDataInBackground(int CodServico, GetServicoFotoCallBack userCallback) {
    new FetchServicoFotoDataAsyncTasck(CodServico, userCallback).execute();
}


public class FetchServicoFotoDataAsyncTasck extends AsyncTask<Void, Void, ArrayList<String>> {
    ArrayList<String> ltservico;
    int CodServico;
    GetServicoFotoCallBack servCallback;

    public FetchServicoFotoDataAsyncTasck(int CodServico, GetServicoFotoCallBack servicoCallback) {
        this.CodServico = CodServico;
        this.servCallback = servicoCallback;
}

    @Override
    protected ArrayList<String> doInBackground(Void... params) {
        ArrayList<String> returnedServico = null;
        try {

            URL url = new URL(SERVER_ADDRESS + "myphpfile.php");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("CodServico", this.CodServico+"");
            final String postParameters = builder.build().getEncodedQuery();
            conn.setConnectTimeout(3000);
            conn.setReadTimeout(3000);
            conn.setRequestMethod("POST");
            conn.setFixedLengthStreamingMode(postParameters.getBytes().length);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setDoInput(true);
            conn.setDoOutput(true);

            //send the POST out
            PrintWriter pw = new PrintWriter(conn.getOutputStream());
            pw.print(postParameters);
            pw.close();
            conn.connect();
            String result = convertStreamToString(conn.getInputStream());
            JSONArray jArray = new JSONArray(result);
            JSONObject json_data = null;
            returnedServico = new ArrayList<>();
            for (int i = 0; i < jArray.length(); i++) {
                json_data = jArray.getJSONObject(i);
                String imagem = json_data.getString("FotoServico");
                returnedServico.add(imagem);
            }
        } catch (Exception e) {

            e.printStackTrace();
            Log.e("Exception", "Erro[" + e.getMessage() + "] ");
        }
        return returnedServico;
    }

    @Override
    protected void onPostExecute(ArrayList<String> returnedServico) {
        servCallback.done(returnedServico);
        super.onPostExecute(ltservico);
    }
}

private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

MyActivity.java:

public class verfotoserv extends BaseNavegationActivity {

private LinearLayout lnrVerImages;
private ImageView imageView;
int codservico=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.verfotoserv);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    lnrVerImages = (LinearLayout) findViewById(R.id.lnrImages);

    final Bundle extras = getIntent().getExtras();
    if (extras != null) {
        codservico = extras.getInt("CodServico");
    }

    ServerRequests serverRequests = new ServerRequests(this);
    serverRequests.FetchServicoFotoDataInBackground(codservico, new GetServicoFotoCallBack() {
        @Override
        public void done(ArrayList<String> returnImagens) {
            try {
                if (returnImagens == null) {
                    throw new Exception("Não existem dados ou ocorreu um erro no servidor\nTente novamente mais tarde.");
                }
                for (String imagem : returnImagens) {
                    Log.i("ImagemRecebida",imagem);
                    byte[] imageAsBytes = Base64.decode(imagem.getBytes(), Base64.DEFAULT);
                    imageView = new ImageView(verfotoserv.this);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                    params.setMargins(5, 5, 5, 5);
                    imageView.setLayoutParams(params);

                    imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes,0,imageAsBytes.length));
                    imageView.setAdjustViewBounds(true);
                    lnrVerImages.addView(imageView);
                }
            }
            catch (Exception erro){
                erro.printStackTrace();
                showError(erro);
            }
        }
    });

}

private void showError(Exception erro){
    Log.e("Erro", erro.getMessage());
    AlertDialog.Builder dialogBuilder=new android.app.AlertDialog.Builder(verfotoserv.this);
    dialogBuilder.setMessage("Erro:"+erro.getMessage());
    dialogBuilder.setPositiveButton("Ok", null);
    dialogBuilder.show();
}
}

MyActivity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/lnrVerImages"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>
</ScrollView>
</LinearLayout>

Myphpfile:

<?php
include_once "ligarbd.php";

$codservico = $_POST['CodServico'];

$SQL = "select * from table where CodServico=".$codservico;
$result = mysql_query($SQL);

$imagens = array();
$i = 0;
while ( $postData = mysql_fetch_assoc($result) ) {
    $imagens[$i][FotoServico]=base64_encode($postData['FotoServico']);
    $i = $i + 1;
}

echo json_encode($imagens);
?>
André Marques
  • 180
  • 1
  • 15
  • Are those strings saved enitre on the DB? – Linuslabo May 25 '16 at 08:28
  • when i save in database i decode from base64 and then i save them @Linuslabo – André Marques May 25 '16 at 08:31
  • I suppose you mean "encode to base64". If those strings are not saved entire on the DB (some part of the strings is missing already in the DB), it may be due to the limit of post request size (http://stackoverflow.com/questions/2364840/what-is-the-size-limit-of-a-post-request). What's the size of the image? – Linuslabo May 25 '16 at 08:34
  • @Linuslabo what i got in my db is completely fine i checked that what its not is what is in my android app i cant get the full string – André Marques May 25 '16 at 09:00

1 Answers1

0

It was my mistake it was receiving everything just needed to change this

lnrVerImages = (LinearLayout) findViewById(R.id.lnrImages);

To this

lnrVerImages = (LinearLayout) findViewById(R.id.lnrVerImages);
André Marques
  • 180
  • 1
  • 15