0

Main Activity I am taking picture and storing here and passing the path to makeimagerequest() function in JSONParser class which makes http request to url

Camera.PictureCallback getPictureCallback = new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                pass=saveImage(bitmap);
                new send_to_server().execute();

            }
        };

saveImage function

public String saveImage(Bitmap myBitmap) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
        File wallpaperDirectory = new File(
                Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);


        if (!wallpaperDirectory.exists()) {
            Log.d("directory", "" + wallpaperDirectory.mkdirs());
            wallpaperDirectory.mkdirs();
        }

        try {
            File f = new File(wallpaperDirectory, "abc"+ ".jpg");
            f.createNewFile();   //give read write permission
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
            MediaScannerConnection.scanFile(this,
                    new String[]{f.getPath()},
                    new String[]{"image/jpeg"}, null);
            fo.close();
            Log.d("TAG", "File Saved:" + f.getAbsolutePath());

            return f.getAbsolutePath();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return "";

    }

Inside send_to_server asynchronous class I am doing the following:

String json = jsonParser.makeImageRequest(url,
                    "POST", pass);

makeImageRequest function in JSONParser class:

public String makeImageRequest(String url, String method,
                                    String image) {


        try {
            json="";


            MultipartEntityBuilder builder = MultipartEntityBuilder.create();


            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);


            FileBody fileBody = new FileBody(new File(image)); 
            builder.addPart("my_file", fileBody);
            HttpEntity entity = builder.build();

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            httpPost.setEntity(entity);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }


        return json;

    }

Finally on server I am doing the following:

<?php
$response=array();
$filePath = "/lib/test.jpg";
$imsrc = $_FILES['my_file']['tmp_name'];
move_uploaded_file($imsrc,$filePath);
$response["success"] = 1;
        $response["message"] = "uploaded";
echo json_encode($response);
?>
aryanknp
  • 1,135
  • 2
  • 8
  • 21

0 Answers0