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", Cloudinary.emptyMap());
.
Here is my code:
public class Camera extends ActionBarActivity implements View.OnClickListener {
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
b= (Button) findViewById(R.id.button);
b.setOnClickListener(this);
}
public static final int TAKE_PHOTO_REQUEST = 0;
@Override
public void onClick(View v) {
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);
Map config = new HashMap();
config.put("cloud_name", "dkepfkeuu");
config.put("api_key", "552563677649679");
config.put("api_secret", "7n8wJ42Hr_6nqZ4aOMDXjTIZ4P0");
Cloudinary cloudinary = new Cloudinary(config);
try {
cloudinary.uploader().upload("", Cloudinary.emptyMap());
} catch (IOException e) {
e.printStackTrace();
}
}
}
On the settings page, click on the upload tab, and scroll down to the upload presets section . Click on the bold text that says Enable unsigned uploading, this allows users to upload images and other assets into your Cloudinary account without pre-signing the upload request.
Cloudinary supports specifying a folder name while uploading by adding the folder parameter to your upload API call. When you use the folder parameter, the public ID given to the newly uploaded image will begin with the folder name you provided and continue based on the other options you provided for naming the file.
Upload endpoint: https://api.cloudinary.com/v1_1/${cloudName}/upload . To use the endpoint in your application, write a function that calls the Cloudinary upload endpoint and pass: an unsigned upload preset with the upload method options you want to apply for all files. the file(s) to upload.
The order is like this: Take picture->save to file->upload to cloudinary Here is the code:
File photoFile;
@Override
public void onClick(View v) {
Intent takePhotoIntent = new Intent(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 created
if (photoFile != null) {
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_TAKE_PHOTO) {
if (resultCode == RESULT_OK) {
//File to upload to cloudinary
Map config = new HashMap();
config.put("cloud_name", "dkepfkeuu");
config.put("api_key", "552563677649679");
config.put("api_secret", "7n8wJ42Hr_6nqZ4aOMDXjTIZ4P0");
Cloudinary cloudinary = new Cloudinary(config);
try {
cloudinary.uploader().upload(photoFile.getAbsolutePath(), Cloudinary.emptyMap());
} catch (IOException e) {
e.printStackTrace();
}
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
//finish();
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "capturedImage";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
return 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With