Ho un database remoto con MySQL e sto memorizzando le foto degli utenti della mia app sul database come una riga del database con il tipo LONGTEXT.Come utilizzare BLOB con JSON e PHP?
Trasformo le foto in una stringa con Base64.
Mi collego al mio database remoto con JSON e PHP, perché questo, devo usare Base64, perché come so, JSON e PHP devono inviare stringhe sui parametri, e con Base64 posso trasformare la foto in un stringa.
Funziona bene, ma è molto lento. Quando sto caricando una foto di 100 KB, ci vuole un sacco di tempo, ma quando sto caricando una foto di 5 KB ci vogliono solo due o tre secondi.
Un amico mi ha detto di utilizzare BLOB invece di Base64, ma come utilizzare BLOB con JSON e una connessione PHP al database? Inoltre, ho bisogno di memorizzare le immagini su una riga del tavolo USER
. Questo perché gli utenti non hanno i privilegi per caricare i file nel server remoto, ma possono caricare le foto caricandole come una stringa in una riga della tabella USER
.
grazie
EDIT:
questo è il codice dove ci vuole un tempo looot attesa (attende nella linea: while ((line = reader.readLine()) != null) {
, che è in attesa reader.readLine()
)
questo codice ottiene un utente dal database remoto, ci vuole un loooooot di tempo per mostrare all'utente il mio app
public Friend RetrieveOneUser(String email)
{
Friend friend=null;
String result = "";
//the parameter data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email",email));
//http post
InputStream is=null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(this.BaseURL + this.GetOneUser_URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
friend=new Friend(json_data.getString("email"),json_data.getString("password"), json_data.getString("fullName"), json_data.getString("mobilePhone"), json_data.getString("mobileOperatingSystem"),"",json_data.getString("photo"));
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return friend;
}
La memorizzazione delle immagini stesse nel database (piuttosto che la semplice memorizzazione di un riferimento a un file nel filesystem remoto) è probabilmente una cattiva mossa. C'è una domanda molto buona che esamina questo argomento in dettaglio: [Memorizzazione di immagini in DB - Sì o Nay?] (Http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or- no) –
non riesco a memorizzare un riferimento a un file nel filesystem remoto perché gli UTENTI della APP non possono caricare foto nel sistema remoto ..... l'ho detto nella mia domanda ¿perché qualcuno mi ha messo un voto negativo ?? – NullPointerException
perché gli utenti non possono caricare foto nel sistema? – binnyb