捕获的图像显示,并发送至服务器的android [英] Capture image show and send to server in android

查看:104
本文介绍了捕获的图像显示,并发送至服务器的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我开发这个机器人中的2.3.3

 保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == CAPTURE_IMAGE_ACTIVITY_REQUEST_ code){
        如果(结果code == RESULT_OK){
             。位图照片=(位图)data.getExtras()获得(数据);
             imageView.setImageBitmap(照片);
             意向意图= getIntent();
            的System.out.println(映像名称========================= ::+ imageName);
            requestIntent.putExtra(imageName,imageName);
            startActivity(requestIntent);

        }否则,如果(结果code == RESULT_CANCELED){
            //用户取消了图像采集
        } 其他 {
            //图像捕捉失​​败,提醒用户
        }
    }
}
 

图片没有显示在的ImageView 而是存储在 SD卡。尽管通过调用另一个意图正在发送的图像它显示我即 A错误12月11日至1日:00:15.085:E / log_tag(9205):错误的HTTP连接android.os.NetworkOnMainThreadException 并且意图code为。

 公共类previewAndSendImage延伸活动{

@燮pressLint(SdCardPath)
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    按钮发送=(按钮)findViewById(R.id.sendToServer);
    send.setEnabled(真正的);
    send.setOnClickListener(新View.OnClickListener(){

        公共无效的onClick(视图v){
            意向意图= getIntent();
            字符串名称= intent.getStringExtra(imageName);
            的System.out.println(映像名称:::::::::::::::: ++姓名);
            位图bitmapOrg = BitmapFactory.de codeFILE(/ SD卡/图片/+姓名);
            ByteArrayOutputStream宝=新ByteArrayOutputStream();
            bitmapOrg.com preSS(Bitmap.Com pressFormat.JPEG,90,宝);
            byte []的BA = bao.toByteArray();

            字符串BA1 = Base64.en codeBytes(BA);
            ArrayList的<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();
            nameValuePairs.add(新BasicNameValuePair(REQID,40));
            nameValuePairs.add(新BasicNameValuePair(形象,BA1));

            尝试{

                     HttpClient的HttpClient的=新DefaultHttpClient();
                     HttpPost httppost =新HttpPost(http://192.168.0.115:8085/Dplus_EB/bill);
                     httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
                     HTT presponse响应= httpclient.execute(httppost);
                    // HttpEntity实体= response.getEntity();
                    //是= entity.getContent();
                     的BufferedReader RD =新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent()));
                      串线=;
                      而((行= rd.readLine())!= NULL){
                          的System.out.println(成功----------------------------);
                          Toast.makeText(previewAndSendImage.this,状态:+线,Toast.LENGTH_LONG).show();
                      }
            }赶上(例外五){

                    Log.e(log_tag,错误的HTTP连接+ e.toString());

            }

        }
    });

}
  }
 

解决方案

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

第1步:一类在你的应用程序,捕捉照片的相机,并将其保存到你的SD卡目录。看到这个<一href="http://adblogcat.com/camera-api-simple-way-to-take-pictures-and-save-them-on-sd-card/">Link,又见这个讨论<一个href="http://stackoverflow.com/questions/6027720/android-how-to-save-captured-image-into-phones-gallery">discussion.

第2步:现在让Android的FTP客户端。请参阅此链接

第3步:现在你可以做出许多类型的交互与FTP服务器一样changeDir,上传文件,下载文件。看到这个<一href="http://zanailhan.blogspot.in/2012/03/android-connect-to-ftp-server-example.html?showComment=1351014353409#c3572419283443761355">Tutorial.

这是简单的,但唯一的条件是,你必须阅读一些文件。请接受并投了我的答案,如果你发现它有用。并且还评论我,如果任何一个环节没有奏效。谢谢你。

Hello I Want to capture image by click a button and after the photo was taken it will show on the screen and then click another button which will send that image to server. i tried it in many ways but i didn't found any appropriate solution so please help me.

I am developing this in 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
        }
    }
}

the image is not showing in that ImageView but stored in SdCard. And while am sending that image by calling another intent it shows me a error i.e 11-01 12:00:15.085: E/log_tag(9205): Error in http connection android.os.NetworkOnMainThreadException and that intent code is.

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

            }

        }
    });

}
  }

解决方案

Please follow the simple steps below and you can easily achieve what you want:

Step 1 : Make a class in your app that capture photo from camera and save it to your sd-card directory. See this Link, see also this discussion discussion.

step 2 :Now make Android FTP client. See this link.

step 3 : Now you can make many types of interaction with FTP server like changeDir, Upload files, Download file. See this Tutorial.

It is simple but only condition is that you have to read some documents. Please accept and vote up my answer if you find it helpful. And also comment me if any link not worked. Thanks.

这篇关于捕获的图像显示,并发送至服务器的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆