How to delete cancelled video - android

I am using Native Camera intent to capture video. In Nexus S If i capture a video then whether i cancel or pressed ok always the video file is getting store in default Medi URI path. But I have a requirement to delete the captured video when user clicks ok. I am using
following code to call camera
Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(videoIntent, CAPTURE_VIDEO);
and following ciode handles cancel button click event
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
if (requestCode == CAPTURE_VIDEO) {if(resultCode == Activity.RESULT_CANCELED)
//pointer comes here successfully. It tells that cancel button is clicked. But I am unabelt to know how to delete the currently cancelled video
}
}
}

Ok, Its some ugly way..
First get the real path of the video from returned URI, like
private String videoPath = "";
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_VIDEO)
{
Uri vid = data.getData();
videoPath = getRealPathFromURI(vid);
}
}
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Videos.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Videos.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Now you have a real path of video file then using file operation you can delete it.
File videoFile = new File(videoPath);
videoFile.deleteOnExit();
I doubt if there is available any method to delete it form android database..

Related

Get image from camera without pressing ok button after camera click in android

I did code for capturing the image from camera and it works fine,
After capturing the image it is asking for click ok in camera but i want to get image without clicking on ok button. my code for is as below and i don't have idea to get image without clicking ok button so please help me.
button_camera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Log.e("PATH", filePath+"");
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
}
}
};
On button click listerner write the following code
cameraBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
To achieve this, we should notify the camera intent to enable quick capture mode, while calling it. Below the code:
private static final int REQUEST_IMAGE_CAPTURE = 1;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra("android.intent.extra.quickCapture", true); // enables single click image capture
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
Note: I learned from stackoverflow and few other sites people say some devices do not support this mode. And I am not sure what devices those are. To this date, I've tested on devices from different brands with API levels 21 to 28, which all has worked for me so far.

How to have the option of having a user add an image in android

I am developing an android application for the first time and I was wondering how can I have a option of having the user upload an image. Like for example, in a contact manager the user has the option of uploading an image of a contact. I was wondering how can I do that in my android application. Any help would be appreciated.
You can start from scratch..
1)Take a look at INTENT
Android Intent
Then take a look at this blogpost
Image Upload
Hope this helps you
In this one you want to click the image view to get image from sd card.. If you want to change to button then replace the listener from image to button.
int RESULT_LOAD_IMAGE = 1;
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
getting image from sd card:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}

Large image Extra in Intent, cause black Screen in Android

I have this code to pick the images from gallery or camera:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SELECT_PICTURE_ACTIVITY_REQUEST_CODE:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
ext = filePath
.substring(filePath.lastIndexOf(".") + 1);
photod = BitmapFactory.decodeFile(filePath);
new AsyncTaskOne().execute(new String[] {});
(fileNameIndex);
}
}
break;
case SELECT_CAMERA_ACTIVITY_REQUEST_CODE:
if (requestCode == CAMERA_REQUEST) {
photod = (Bitmap) data.getExtras().get("data");
new AsyncTaskOne().execute(new String[] {});
}
}
}
When I press the confirm button I invoke this listener
OnClickListener confirm = new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent("com.striget.eu.UpdatePhoto");
i.putExtra(PropertiesUpdatedPhoto.EXTRA_PHOTO, codedPhoto);
i.putExtra(PropertiesUpdatedPhoto.EXTRA_EXT, ext);
startActivity(i);
}
};
(where codedPhoto is the image coded in base64 by another method)
to send the image and its extension in another activity
Everything works fine with small images but if I chose a medium size image or large photo(also if isn't very large), the app freezes, the screen becomes black and If I wait some minutes return on the current activity without showing any error in the stack and the invoked intent PropertiesUpdatedPhoto doesn't start.
How I could fix this problem?
This problem is discuss here, we should keep intent extra content as small as possible.
My suggestion is instead of passing image, perhaps passing the image URI or Path would be a better solution. Then only load the image in that Activity.
Example :
OnClickListener confirm = new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent("com.striget.eu.UpdatePhoto");
i.setData(PropertiesUpdatedPhoto.EXTRA_PHOTO, # Image URI Here # );
// Or i.putExtra(PropertiesUpdatePhoto.EXTRA_PHOTO, # Image Path Here #);
i.putExtra(PropertiesUpdatedPhoto.EXTRA_EXT, ext);
startActivity(i);
}
};

When pressed back button, i got Force Close in Android

in my activity, i can go to gallery and can pick image. After that i can go back to previous sreecn. But when i went to gallery, if i wouldn't pick an image and press to back button, i can not go to previous screen and i got force close. How can fix that, without using startActivity(intent) ?
Here is my code :
first i defined to
private static final int ACTIVITY_REQUEST_PICK_ATTACHMENT = 1;
On Activity Result Code:
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mAttachments = (LinearLayout) findViewById(R.id.attachments);
switch (requestCode) {
case ACTIVITY_REQUEST_PICK_ATTACHMENT:
Uri _uri = data.getData();
addAttachment(_uri);
Cursor cursor = getContentResolver()
.query(_uri,
new String[] { android.provider.MediaStore.Images.ImageColumns.DATA },
null, null, null);
cursor.moveToFirst();
File imageFilePath = new File(cursor.getString(0));
uris.add(imageFilePath);
names.add(imageFilePath.getName());
Log.v("imageFilePath", imageFilePath.toString());
break;
i call the that at here:
private void onAddAttachment2(final String mime_type) {
// setContentView(R.layout.main);
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType(mime_type);
startActivityForResult(Intent.createChooser(i, null),
ACTIVITY_REQUEST_PICK_ATTACHMENT);
}
the error on my LogCat
05-20 13:16:39.809: E/AndroidRuntime(374): at my.mail.SenderActivity.onActivityResult(KepenekActivity.java:294)
when i double click to error, it shows the line
Uri _uri = data.getData();
logically it is true, my _uri is empty, how can i show the previous screen with this final state here is my problem.
You need to add a check for the resultcode.
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mAttachments = (LinearLayout) findViewById(R.id.attachments);
switch (requestCode) {
case ACTIVITY_REQUEST_PICK_ATTACHMENT:
if (resultCode == RESULT_OK) { // <------ THIS LINE IS IMPORTANT
Uri _uri = data.getData();
addAttachment(_uri);
Cursor cursor = getContentResolver()
.query(_uri,
new String[] { android.provider.MediaStore.Images.ImageColumns.DATA },
null, null, null);
cursor.moveToFirst();
File imageFilePath = new File(cursor.getString(0));
uris.add(imageFilePath);
names.add(imageFilePath.getName());
Log.v("imageFilePath", imageFilePath.toString());
}
break;
If you press the back button instead of choosing something the resultCode will get set to RESULT_CANCELLED instead of RESULT_OK. You can use that distinction to do whatever you need to in either case.

Learning the path of the captured image in onActivityResult method

#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picture_choice_activity);
Button takePictureButton = (Button)findViewById(R.id.takePictureButton);
takePictureButton.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, IMAGE_CAPTURE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == IMAGE_CAPTURE){
if(resultCode == Activity.RESULT_OK){
// I should get the path here
}else if(resultCode == Activity.RESULT_CANCELED){
}
}
}
In the above code I am trying to get the path of the captured image. I should not use EXTRA_OUTPUT to specify a custom path to save the image. How can I do that?
Use this in ur onActivity result
final Uri contentUri = data.getData();
final String[] proj = { MediaStore.Images.Media.DATA};
final Cursor cursor = managedQuery(contentUri, proj, null, null, null);
final int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToLast();
final String path = cursor.getString(column_index);
Kindly check, http://developer.android.com/guide/topics/media/camera.html#intents
MediaStore.EXTRA_OUTPUT : This setting requires a Uri object specifying a path and file name where you'd like to save the picture. This setting is optional but strongly recommended. If you do not specify this value, the camera application saves the requested picture in the default location with a default name, specified in the returned intent's Intent.getData() field.
so, check the Intent data in onActivityResult()

Categories

Resources