Android:拍照以使用WebService发送它们

时间:2014-12-16 07:47:05

标签: android

我想拍摄一些照片,并在完成后,将所有这些照片发送到WebService上,而不将其保留在设备上。

我已经尝试在发送后删除它们,但我没有成功。

这样做的最佳方式是什么?

这是我的图像捕捉活动。

private ImageView imgPreview;
private Button btnCapturePicture;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_capture);

    context = getApplicationContext();
    imgPreview = (ImageView) findViewById(R.id.imgPreview);
    btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);

    /**
     * Capture image button click event
     * */
    btnCapturePicture.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // capture picture
            captureImage();
        }
    });

}


GPSTracker gps;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        //TODO
        gps = new GPSTracker(ImageCaptureActivity.this);
        if (resultCode == RESULT_OK) {
            // successfully captured the image
            // display it in image view
            previewCapturedImage();

            //get gps location coordinates
            if(gps.canGetLocation())
            {
                double latitude = gps.getLatitude();
                double longitude = gps.getLongitude();

                Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: "
                        + longitude, Toast.LENGTH_LONG).show();

                //from latitude and longitude to address
                Geocoder gcd = new Geocoder(context, Locale.getDefault());
                List<Address> addresses = null;
                try {
                    addresses = gcd.getFromLocation(latitude, longitude, 1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (addresses.size() > 0) {
                    String tara = addresses.get(0).getCountryName();
                    String judet = addresses.get(0).getLocality();
                    String oras = addresses.get(0).getSubLocality();
                    String adresa = addresses.get(0).getAddressLine(0);
                    System.out.println(tara + ", " + judet + ", " + oras + ", " + adresa + "\n");

                }
            } else {
                // Can't get location.
                // GPS or network is not enabled.
                // Ask user to enable GPS/network in settings.
                Toast.makeText(getApplicationContext(), "Turn on GPS", Toast.LENGTH_LONG).show();
                Intent backIntent = new Intent(ImageCaptureActivity.this, Cont.class);
                startActivity(backIntent);
            }


            //TODO
            //GET DATE OF IMAGE
            String pathToFile = fileUri.getPath();
            File file = new File(pathToFile);
            if(file.exists()) {
                String date = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss").format(
                        new Date(file.lastModified())
                );
                Toast.makeText(context, date, Toast.LENGTH_LONG)
                        .show();
            }



        } else if (resultCode == RESULT_CANCELED) {
            // user cancelled Image capture
            Toast.makeText(getApplicationContext(),
                    "Cancelled", Toast.LENGTH_SHORT)
                    .show();
        } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                    "Error!", Toast.LENGTH_SHORT)
                    .show();
        }
    }

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    // save file url in bundle as it will be null on scren orientation
    // changes
    outState.putParcelable("file_uri", fileUri);
}

/*
 * Here we restore the fileUri again
 */
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    // get the file url
    fileUri = savedInstanceState.getParcelable("file_uri");
}

辅助方法:

    private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

}

/**
 * Creating file uri to store image/video
 */
public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/*
 * returning image / video
 */
private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "file"
                    + IMAGE_DIRECTORY_NAME + " was not created");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFileName;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFileName = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFileName;
}

/*
 * Display image from a path to ImageView
 */
private void previewCapturedImage() {
    try {
        imgPreview.setVisibility(View.VISIBLE);

        // bimatp factory
        BitmapFactory.Options options = new BitmapFactory.Options();

        // downsizing image as it throws OutOfMemory Exception for larger
        // images
        options.inSampleSize = 8;

        final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                options);

        imgPreview.setImageBitmap(bitmap);

    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

我想在REST WebService上发送照片。

1 个答案:

答案 0 :(得分:0)

感谢您的编辑!

首先,保存图像并删除图像的方法是正确的,因为将图像保存在内存中并不是一个好习惯。

其次,我假设您在Web服务器上运行Web REST服务。因此,如果要上传图像,则必须使用正确的请求方法对服务器所服务的端点进行HTTP调用。您可以查看here以获得更精确的代码示例,了解如何进行确切的调用。

最后,我建议你在前台服务中执行繁重的上传工作,这样你就可以确保操作系统不会在上传过程中杀死你的线程。