我想裁剪从相机拍摄的图像并在ImageView中设置该图像,之后我想将其发送到服务器中的文件夹。 目前我能够在ImageView中设置裁剪的图像,并且该图像正在我的SD卡上保存。 但是我没有得到如何在点击按钮时将其保存在服务器中。请帮助我。这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_update);
Button captureBtn = (Button) findViewById(R.id.SelectImageBtn);
captureBtn.setOnClickListener(this);
Button captureBtn1 = (Button) findViewById(R.id.btnCapturePicture);
captureBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launchUploadActivity(true);
}
});
}
private void launchUploadActivity(boolean isImage) {
Intent i = new Intent(SaveUpload1.this, UploadActivity.class);
i.putExtra("filePath", picUri.getPath());
i.putExtra("isImage", isImage);
startActivity(i);
}
public void onClick(View v) {
if (v.getId() == R.id.SelectImageBtn) {
try {
// use standard intent to capture an image
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
picUri = getOutputMediaFileUri();
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
// start the image capture Intent
startActivityForResult(captureIntent, CAMERA_CAPTURE);
// we will handle the returned data in onActivityResult
// startActivityForResult(captureIntent, CAMERA_CAPTURE);
} catch (ActivityNotFoundException anfe) {
Toast toast = Toast.makeText(this, "This device doesn't support the crop action!",
Toast.LENGTH_SHORT);
toast.show();
}
}
}
/**
* ------------ Helper Methods ----------------------
* */
/**
* Creating file uri to store image/video
*/
public Uri getOutputMediaFileUri() {
return Uri.fromFile(getOutputMediaFile());
}
/**
* returning image / video
*/
private static File getOutputMediaFile() {
// External sdcard location
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
Config.IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(TAG, "Oops! Failed create "
+ Config.IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_CAPTURE) {
// get the Uri for the captured image
// picUri = data.getData();
performCrop();
}
// user is returning from cropping the image
else if (requestCode == CROP_PIC) {
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
ImageView picView = (ImageView) findViewById(R.id.ProfilePicIV);
picView.setImageBitmap(thePic);
}
}
}
/**
* this function does the crop operation.
*/
private void performCrop() {
// take care of exceptions
try {
// call the standard crop action intent (the user device may not
// support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 2);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, CROP_PIC);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
Toast toast = Toast
.makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
toast.show();
}
}