2012-11-01 54 views
0

你好我想通过点击一个按钮捕捉图像,拍摄照片后它会显示在屏幕上,然后点击另一个按钮,将图像发送到服务器。我在很多方面尝试过,但我没有找到任何合适的解决方案,请帮助我。拍摄图像显示并发送到服务器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()); 

      } 

     } 
    }); 

} 
    } 
+1

AsyncTask的演示示例。 http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – ckpatel

+1

你在哪里卡住了这个过程中捕获图像和存储或发送图像到服务器? – juned

+1

请发布您的logcat错误,如果有的话。 –

回答

6

请按照以下简单的步骤,就可以轻松实现你想要什么:

步骤1:在您的应用程序类,从摄像头取并保存到你的SD卡目录。看到这个Link,另见这个讨论discussion

第2步:现在制作Android FTP客户端。看到这个link

第3步:现在您可以使用多种类型的FTP服务器交互,如changeDir,上传文件,下载文件。看到这个Tutorial

这很简单,但条件是你必须阅读一些文件。如果您觉得有帮助,请接受我的回答并投票。并且如果有任何链接无法正常工作,请评论我谢谢。

1

你不能在ui线程上进行http调用。至少不在较新的操作系统上。

考虑使用AsyncTask进行此类调用。

+0

为什么投了票?他给了一个好的提示IMO。 –

+0

@LazyNinja对,但有人不低调AsyncTask。 upvote,如果你认为正确answer.thanks – ckpatel

+0

@ChiragPatel有人只开放帐户downvoting。 –

相关问题