0

I got this code that takes an image called "dmlo" from drawable and upload it to php server, all I want is instead of taking that picture from drawable I want to take it from an imageview , what should I do ? Should I replace any code in the first line by another one ?

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.dmlo);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new
    HttpPost("http://192.168.1.38/mobileappd/base.php");
    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());
    Toast.makeText(getBaseContext(), e.toString(),Toast.LENGTH_LONG).show();
}
SztupY
  • 9,608
  • 8
  • 63
  • 80

2 Answers2

1

Check this code - it might return null if no drawable is assigned

imageView.getDrawable();
Royi
  • 697
  • 8
  • 21
0

Try this, it returns image in drawable format and after getting image from imageview you can pass object of Drawable to php server.

ImageView mImg1 = (ImageView) findViewById(R.id.mImg1);
// For get Drawable from Image
Drawable d = mImg1.getDrawable();

// For Convert Drawable to Bitmap
Bitmap bitmapOrg = ((BitmapDrawable)d).getBitmap();

it will solve your problem.

Dipak Keshariya
  • 21,618
  • 18
  • 74
  • 127
  • I tried it Dipak I wrote : Drawable d=photo.getDrawable(); // photo is an ImageView It's giving an error in this line : Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),d); The function decodeResource is not taking it as an argument – moonwalker Jan 08 '13 at 13:00
  • @moonwalker write this Bitmap bitmapOrg = ((BitmapDrawable)d).getBitmap(); instead of Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.dmlo); – Dipak Keshariya Jan 08 '13 at 13:08