Je souhaite télécharger une image sur mon serveur Web. J'ai une réponse du serveur et tout, donc ce n'est pas le problème. Lorsque je colle les lignes suivantes dans le code, il se bloque le plus souvent.Android: AsyncTask se bloque lors du traitement de l'image
Je vois une RuntimeException pour AsyncTask dans le débogueur, c'est tout.
Une autre question: puis-je obtenir le bitmap non compressé dans un tableau d'octets, afin que je puisse télécharger le fichier original avec la fin de fichier d'origine?
Voici les lignes de problème:
// create the image for the server
Bitmap bm=BitmapFactory.decodeFile(Values.imageFilePath);
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byte_array=stream.toByteArray();
String byteString=Base64.encodeToString(byte_array, Base64.DEFAULT);
Et voici toute AsyncTask-classe, le code ci-dessus est commenté et il fonctionne. Quel est le problème? (ProgressDialog sera créé dans l'activité principale) u Merci.
class UploadThread extends AsyncTask<String, Void, String>
{
private Context _context;
private ProgressDialog pd;
public UploadThread(Context context)
{
super();
_context=context;
}
protected String doInBackground(String... urls)
{
String response="Android says \"Nothing happened.\"";
InputStream is = null;
StringBuilder sb=null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Values.hostname);
ArrayList<NameValuePair> nvp=new ArrayList<NameValuePair>();
// This is the Post data.
nvp.add(new BasicNameValuePair("plog_android_upload","plogupload"));
nvp.add(new BasicNameValuePair("thecomment",Values.comment));
// get the image and process it.
// Values.progressDialog.setMessage("Processing image..");
// create the image for the server
// Bitmap bm=BitmapFactory.decodeFile(Values.imageFilePath);
// ByteArrayOutputStream stream=new ByteArrayOutputStream();
// bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
// byte[] byte_array=stream.toByteArray();
// String byteString=Base64.encodeToString(byte_array, Base64.DEFAULT);
//Values.progressDialog.setMessage("Uploading..");
//nvp.add(new BasicNameValuePair("upload_file",byteString));
httppost.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity entity = httpresponse.getEntity();
is = entity.getContent();
response="Connection established.";
}catch(UnknownHostException eu){ // gets thrown on unknown host.
return "No connection!\n(or wrong hostname)";
}catch(HttpHostConnectException ex){ // gets thrown on unknown connection.
return "No connection!\n(or wrong hostname)";
}catch(Exception e){
return "Error in http connection:"+e.toString();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
response="Server says: "+sb.toString();
}catch(Exception e){
response= "Error converting HTTP result: "+e.toString();
return response;
}
return response;
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
// dismiss the progress dialog.
Values.progressDialog.dismiss();
// Show the user some feedback.
Toast.makeText(_context,result, Toast.LENGTH_LONG).show();
}
}
après la trace de la pile avec le numéro de ligne exact. –
votre problème est ici Values.progressDialog.setMessage ("Uploading .."); car il s'agit d'une interface utilisateur et l'interface utilisateur ne peut pas être touchée à partir doInBackground. et YES après une trace de pile – Yazan