如何捕获图像并在android中使用json发布

时间:2013-12-06 04:36:37

标签: android web-services camera

我有两个网址,一个用于捕获后的图片名称,另一个用于该位置的商店图片。现在我完成了开放式摄像机的处理。当我拍照时,图像存储在设备手机存储目录上的DCIM / CAMARA上。但图像不会发布并存储在两个不同的URL上。我能做些什么呢?

Open Camara代码

 btn_camera = (Button)findViewById(R.id.btn_camera);
         btn_camera.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                 startActivityForResult(intent, RESULT_LOAD_IMAGE); 

//               post(Image_URL, null);

            }
        });

>关于网址发布数据的代码

try{
                      ArrayList<NameValuePair> mNameValuePair = new ArrayList<NameValuePair>();
                      mNameValuePair.add(new BasicNameValuePair("lid", lotids));
                      mNameValuePair.add(new BasicNameValuePair("aid", attandant_id));
                      mNameValuePair.add(new BasicNameValuePair("vnum", vehicle_number));
                      mNameValuePair.add(new BasicNameValuePair("imag", "xyz"));

                      Log.i("NameValuePair","" + mNameValuePair);

                      result = mCommonClass.PostConnection(Issue_Summons_Url, mNameValuePair);
                      Log.i("result for log",""+ result);

                  }
                  catch (Exception e) {
                      // TODO: handle exception
                      e.printStackTrace();
                      Log.e("FastPark App","Nothing to be display");
                  }

4 个答案:

答案 0 :(得分:0)

您可以从相机意图中找到该文件的uri并将其转换为二进制数据并将其上传到Web服务 How to upload a image to server using HttpPost in android?,或者如果您想将其存储在DCIM / CAMERA以外的其他位置,可以参考此网页http://developer.android.com/training/camera/photobasics.html

答案 1 :(得分:0)

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
    // get image from camera
            case REQUEST_CODE_CAMERA_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    // Uri cameraImageUri = data.getData();
                    Log.e("", "camera uri= " + Uri.fromFile(getFile()));
    String path=getFile().getAbsolutePath();
prepareTouploadImage(path);

                }
                break;
    default;
    break;

此路径返回您的相机图像路径.. 这条路径将发布在json。

/**
     * encodes image to string using Base64
     * 
     * @return
     */
    private String prepareTouploadImage(String profileImagePath) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(profileImagePath, options);
        bitmap=Bitmap.createScaledBitmap(bitmap, 50, 50, true);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 50, baos);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);


        byte[] byteArray = baos.toByteArray();

        String imageString = com.eg_lifestyle.utils.Base64
                .encodeBytes(byteArray);
        bitmap = null;
        System.gc();
        Runtime.getRuntime().gc();
        return imageString;
    }

以上方法返回转换为二进制的图像并返回字符串

答案 2 :(得分:0)

private void doFileUpload(){
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream inStream = null; 
    String exsistingFileName = "/sdcard/six.3gp";
    // Is this the place are you doing something wrong.
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;
    String urlString = "http://192.168.1.5/upload.php";
    try
    {
        Log.e("MediaPlayer","Inside second Method");
        FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
        URL url = new URL(urlString);
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        // Allow Outputs
        conn.setDoOutput(true);
        // Don't use a cached copy.
        conn.setUseCaches(false);
        // Use a post method.
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
        dos = new DataOutputStream( conn.getOutputStream() );
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
        dos.writeBytes(lineEnd);
        Log.e("MediaPlayer","Headers are written");
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        while (bytesRead > 0)
        {
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            tv.append(inputLine);
        // close streams
        Log.e("MediaPlayer","File is written");
        fileInputStream.close();
        dos.flush();
        dos.close();
    }
    catch (MalformedURLException ex)
    {
        Log.e("MediaPlayer", "error: " + ex.getMessage(), ex);
    }
    catch (IOException ioe)
    {
        Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe);
    }

    //------------------ read the SERVER RESPONSE
    try {
        inStream = new DataInputStream ( conn.getInputStream() );
        String str;            
        while (( str = inStream.readLine()) != null)
        {
            Log.e("MediaPlayer","Server Response"+str);
        }
        /*while((str = inStream.readLine()) !=null ){

        }*/
        inStream.close();
    }
    catch (IOException ioex){
        Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex);
    }

使用此功能....我希望将图像存储在服务器上的特定文件夹中对您有用

答案 3 :(得分:0)

public void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
    // get image from camera
            case REQUEST_CODE_CAMERA_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    // Uri cameraImageUri = data.getData();
                    Log.e("", "camera uri= " + Uri.fromFile(getFile()));
    String path=getFile().getAbsolutePath();
prepareTouploadImage(path);

                }
                break;
    default;
    break;

}

在你的主要活动中使用这个...你可以从图像中获取路径