拍照&在Android中上传到服务器

时间:2014-09-24 07:20:24

标签: java android http http-post

我试图在Android上拍照并使用POST方法将其发送到服务器。我在stackoverflow上搜索了很多,我现在的代码基于其他答案,但我仍然无法发送到服务器。图像的参数名称应为" file"在我的POST请求中。我调试了,响应为空,contentlegth为-1。这是我的代码:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_upload_data, container, false);
    TextView barcodeText = (TextView) v.findViewById(R.id.label);
    Button picButton = (Button) v.findViewById(R.id.take_picture);
    picButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            String file = "img.jpg";
            File newfile =  new File(Environment.getExternalStorageDirectory(), file);
            file= newfile.getAbsolutePath();
            Uri outputFileUri = Uri.fromFile(newfile);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            cameraIntent.putExtra("return-data", true);
            startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
        }
    });
    barcodeText.setText(barcode);
    return v;
}

这是我的onActivityResult:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == TAKE_PHOTO_CODE) {
        Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/img.jpg");
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream); //compress to which format you want.
        byte [] byte_arr = stream.toByteArray();
        String image_str = Base64.encodeBytes(byte_arr);
        nameValuePairs.add(new BasicNameValuePair("file",image_str));
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                try{
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost(Constants.fakeURL);
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    final String the_string_response = convertResponseToString(response);
                    getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                          Toast.makeText(getActivity().getApplicationContext(), "Response "+the_string_response , Toast.LENGTH_LONG).show();
                        }
                    });

                }catch(final Exception e){
                    getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                               Toast.makeText(getActivity().getApplicationContext(), "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    });
                    System.out.println("Error in http connection "+e.toString());
                }
            }
        });
        t.start();
    }

我的课程中也有BASE64.java。谁能告诉我什么是错的?

1 个答案:

答案 0 :(得分:1)

尝试将位图设置为imageview以确保图像位图在那里,之后使用" BitmapFactory.Options"从这里http://developer.android.com/training/displaying-bitmaps/load-bitmap.html以避免内存不足错误。

编辑:也尝试此压缩。

String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);