0

我得到了这个代码,它从一个image名为“dmlo”的地方drawable上传到 php 服务器,我想要的不是从 drawable 中获取那张图片,而是从一个imageview中获取它,我应该怎么做?我应该用另一个替换第一行中的任何代码吗?

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();
}
4

2 回答 2

1

检查此代码 - 如果未分配可绘制对象,它可能会返回 null

imageView.getDrawable();
于 2013-01-08T12:43:39.600 回答
0

试试这个,它以drawable格式返回图像,从imageview获取图像后,你可以将Drawable的对象传递给php服务器。

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();

它会解决你的问题。

于 2013-01-08T12:42:40.183 回答