你好我想通過點擊一個按鈕捕捉圖像,拍攝照片後它會顯示在屏幕上,然後點擊另一個按鈕,將圖像發送到服務器。我在很多方面嘗試過,但我沒有找到任何合適的解決方案,請幫助我。拍攝圖像顯示併發送到服務器android
我在安卓2.3.3
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
Intent intent= getIntent();
System.out.println("image name=========================:: "+imageName);
requestIntent.putExtra("imageName", imageName);
startActivity(requestIntent);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
開發這個圖像不是在ImageView
顯示,但存儲在SdCard
。而當我通過調用另一個意圖發送該圖像時,它會顯示錯誤,即11-01 12:00:15.085: E/log_tag(9205): Error in http connection android.os.NetworkOnMainThreadException
並且該意圖代碼是。
public class PreviewAndSendImage extends Activity{
@SuppressLint("SdCardPath")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button send=(Button) findViewById(R.id.sendToServer);
send.setEnabled(true);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent= getIntent();
String name=intent.getStringExtra("imageName");
System.out.println("image name :::::::::::::::: +" +name);
Bitmap bitmapOrg = BitmapFactory.decodeFile("/sdcard/pictures/"+name);
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("reqId", "40"));
nameValuePairs.add(new BasicNameValuePair("image",ba1));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.115:8085/Dplus_EB/bill");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
//HttpEntity entity = response.getEntity();
//is = entity.getContent();
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println("success----------------------------");
Toast.makeText(PreviewAndSendImage.this, "Status: "+line, Toast.LENGTH_LONG).show();
}
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
});
}
}
AsyncTask的演示示例。 http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – ckpatel
你在哪裏卡住了這個過程中捕獲圖像和存儲或發送圖像到服務器? – juned
請發佈您的logcat錯誤,如果有的話。 –