Java 从图像视图获取图像并上传到php服务器

Java 从图像视图获取图像并上传到php服务器,java,android,image-uploading,Java,Android,Image Uploading,我从drawable获取了一个名为“dmlo”的图像的代码,并将其上传到php服务器,我只想而不是从drawable拍摄照片我想从图像视图拍摄它,我该怎么办?我应该用另一个代码替换第一行中的任何代码吗 Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.dmlo); ByteArrayOutputStream bao = new ByteArrayOutputStream(); bitmapOrg.c

我从
drawable
获取了一个名为“dmlo”
图像的代码,并将其上传到php服务器,我只想而不是从drawable拍摄照片我想从
图像视图
拍摄它,我该怎么办?我应该用另一个代码替换第一行中的任何代码吗

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();
}
位图位图位图org=BitmapFactory.decodeResource(getResources(),R.drawable.dmlo);
ByteArrayOutputStream bao=新建ByteArrayOutputStream();
压缩(Bitmap.CompressFormat.JPEG,90,bao);
字节[]ba=bao.toByteArray();
字符串ba1=Base64.encodeBytes(ba);
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“图像”,ba1));
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新
HttpPost(“http://192.168.1.38/mobileappd/base.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
Toast.makeText(getBaseContext(),e.toString(),Toast.LENGTH_LONG).show();
}

试试这个,它以可绘制的格式返回图像,从imageview获取图像后,您可以将可绘制的对象传递给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();

它将解决您的问题。

检查此代码-如果未分配可绘制文件,则可能返回null

imageView.getDrawable();

检查@moonwalker请看我的答案,它会解决你的问题。我试过了,我写了:Drawable d=photo.getDrawable();//photo是一个ImageView,它在这一行中给出了一个错误:位图bitmapOrg=BitmapFactory.decodeResource(getResources(),d);函数decodeResource未将其作为argument@moonwalker编写此位图bitmapOrg=((BitmapDrawable)d).getBitmap();而不是位图bitmapOrg=BitmapFactory.decodeResource(getResources(),R.drawable.dmlo);