Upload Captured Image To Cloudinary Directly In Android
I want to capture a picture and upload it to Cloudinary directly. How can I know the name of the pic to set it at the upload statement cloudinary.uploader().upload('nameofthepic',
Solution 1:
The order is like this: Take picture->save to file->upload to cloudinary Here is the code:
File photoFile;
@OverridepublicvoidonClick(View v) {
IntenttakePhotoIntent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePhotoIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully createdif (photoFile != null) {
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_TAKE_PHOTO) {
if (resultCode == RESULT_OK) {
//File to upload to cloudinaryMapconfig=newHashMap();
config.put("cloud_name", "dkepfkeuu");
config.put("api_key", "552563677649679");
config.put("api_secret", "7n8wJ42Hr_6nqZ4aOMDXjTIZ4P0");
Cloudinarycloudinary=newCloudinary(config);
try {
cloudinary.uploader().upload(photoFile.getAbsolutePath(), Cloudinary.emptyMap());
} catch (IOException e) {
e.printStackTrace();
}
} elseif (resultCode == RESULT_CANCELED) {
// User cancelled the image capture//finish();
}
}
}
private File createImageFile()throws IOException {
// Create an image file nameStringtimeStamp=newSimpleDateFormat("yyyyMMdd_HHmmss").format(newDate());
StringimageFileName="capturedImage";
FilestorageDir= Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
Fileimage= File.createTempFile(
imageFileName, /* prefix */".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intentsreturn image;
}
So you are actually uploading photoFile
which is the actually captured image.
Its nice to put the upload function along with some image transformations(scale,resize) in AsyncTask
because camera images in modern handsets are quite big(in size).
Hope it helps.
Post a Comment for "Upload Captured Image To Cloudinary Directly In Android"