1

最初单击相机按钮时,正在捕获图像,显示保存和丢弃按钮。单击保存时,它会保存在指定的文件夹中,但保存的图像不会上传到服务器。

这是我的代码:

public class FldrActivity extends Activity {
    private static final int CAPTURE_IMAGE = 0;
    InputStream inputStream;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        File folder = new File(Environment.getExternalStorageDirectory() + "/image");
        boolean success = false;
        if (!folder.exists()) {
            success = folder.mkdir();
        }
        String path = folder + "/example.jpg";

        //String path = Environment.getExternalStorageDirectory() + "/image/example2.jpg";
        File file = new File(path);
        Uri outputFileUri = Uri.fromFile( file );
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
        intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );

        startActivityForResult( intent, CAPTURE_IMAGE ); 

        String pathToOurFile = "/sdcard/image/example.jpg";

        Bitmap bitmap = BitmapFactory.decodeFile(pathToOurFile);
        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.encodeToString(byte_arr, 0);
        ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("file",image_str));

        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.xyz.com/image/connection.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            String the_string_response = convertResponseToString(response);
            Toast.makeText(getApplicationContext(), "Response " + the_string_response,
                                Toast.LENGTH_LONG).show();
        } catch(Exception {
            Toast.makeText(getApplicationContext(), "ERROR " + e.getMessage(),
                                                  Toast.LENGTH_LONG).show();
            System.out.println("Error in http connection "+e.toString());
        }
    }

    public String convertResponseToString(HttpResponse response) throws  IllegalStateException, IOException {
        String res = "";
        StringBuffer buffer = new StringBuffer();
        inputStream = response.getEntity().getContent();
        int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..
        Toast.makeText(getApplicationContext(), "contentLength : " + contentLength, Toast.LENGTH_LONG).show();
        if (contentLength < 0) {
        }
        else {
            byte[] data = new byte[512];
            int len = 0;

            try {
                while (-1 != (len = inputStream.read(data)) ) {
                    buffer.append(new String(data, 0, len)); //converting to string and appending  to stringbuffer…..
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                inputStream.close(); // closing the stream…..
            } catch (IOException e) {
                e.printStackTrace();
            }

            res = buffer.toString();     // converting stringbuffer to string…..

            Toast.makeText(getApplicationContext(), "Result : " + res, Toast.LENGTH_LONG).show();
            //System.out.println("Response => " +  EntityUtils.toString(response.getEntity()));
        }

        return res;
    }
}
4

0 回答 0