【问题标题】:Capture image show and send to server in android捕获图像显示并发送到android中的服务器
【发布时间】:2012-11-01 06:47:39
【问题描述】:

您好,我想通过单击一个按钮来捕获图像,拍摄照片后它将显示在屏幕上,然后单击另一个按钮,该按钮将把该图像发送到服务器。我尝试了很多方法,但我没有找到任何合适的解决方案,所以请帮助我。

我正在 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 的演示示例。 stackoverflow.com/questions/6343166/…
  • 您在捕获图像并将图像存储或发送到服务器的过程中卡在哪里??
  • 如果有,请发布您的 logcat 错误。
  • @sitanshu:您应该在询问之前尝试搜索。Chirag 的链接是正确的解决方案。部分上传文件使用AsyncTask(在try catch中)。

标签: android


【解决方案1】:

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

第 1 步:在您的应用程序中创建一个从相机捕获照片并将其保存到您的 sd 卡目录的类。请参阅此Link,另请参阅此讨论discussion

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

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

这很简单,但唯一的条件是您必须阅读一些文件。如果您觉得有帮助,请接受并投票赞成我的回答。如果任何链接不起作用,也可以评论我。谢谢。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • @LazyNinja 对,但有人不理解 AsyncTask。如果您认为正确的答案,请投票。谢谢
    • @ChiragPatel 有人只有开户可以投反对票。
    猜你喜欢
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-13
    相关资源
    最近更新 更多