I want to add "remove" option in image chooser intent - android

I want to add remove option in image chooser intent, I successfully added gallery option and camera option and those are working fine but I want to add remove option, if I choose remove option the image should remove from imageview.
Thanks in advance :)
My Code:
public void edit_profile_pic(View view) {
Uri uri = null;
Intent GalleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent CameraIntent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
CameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
Intent chooserIntent = Intent.createChooser(CameraIntent, "Choose");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {GalleryIntent, CameraIntent});
startActivityForResult(chooserIntent, RESULT_LOAD_IMG);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
ImageView imageView = (ImageView) findViewById(R.id.profile_profile_picture);
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
{
Bitmap bitmap = null;
if(data.getData()!=null)
{
try
{
InputStream stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(bitmap);
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
bitmap=(Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
super.onActivityResult(requestCode, resultCode, data);
}
}

I was able to do this with an intent chooser by creating a new activity with the theme NoDisplay like this:
First create the activity which will be used to notify the main activity that the user wants to delete the image:
public class DeleteProfileImageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent resultIntent = new Intent();
resultIntent.putExtra("remove_image", true);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
}
You can specify the icon and label in the manifest. Make sure you also set the theme to NoDisplay to avoid it covering up the actual activity with the intent chooser.
<activity
android:name=".activity.profilesetup.DeleteProfileImageActivity"
android:label="#string/title_activity_delete_profile_image"
android:icon="#drawable/ic_delete_image_red"
android:theme="#android:style/Theme.NoDisplay"
/>
Then you can add it to the intent chooser. I check if the image has been set previously to avoid adding the option when there is no image.
if (imageSet) {
Intent removeImageIntent = new Intent(activity, DeleteProfileImageActivity.class);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {removeImageIntent} );
}
Then you can listen on activity result for intent data with the key specified in the DeleteProfileImageActivity, currently remove_image
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data.hasExtra("remove_image") && imageSet) {
// remove/reset the image here
}
}

You have to create a alert Dailog. There you can give option, and where you can add remove option. On remove option use this:
else if (options[item].equals("Remove")) {
ImageView.setImageDrawable(null);
dialog.dismiss();
}
For more help you can refer http://www.theappguruz.com/blog/android-take-photo-camera-gallery-code-sample/

Related

Losing Uri between implicit camera intents

I'm putting together a camera app using implicit intents for a college assignment, and I can't figure out what's going wrong with my Uri. I'm assigning a jpg file, then opening the camera using MediaStore. ACTION_IMAGE_CAPTURE as shown below. When I call selectImage(), I want the chosen viewing app to go straight to my recently taken photo. However, although the photo is saved and visible within the gallery, I'm getting an "Item not found" Toast, and I'm looking at an overview of all the image folders. Where am I going wrong?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
photoFile = new File(Environment.getExternalStorageDirectory(),"cameraTest.jpg");
outputFile = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", photoFile);
Log.i("debugging",outputFile.getPath().toString());
}
public void capturePhoto(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFile);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(outputFile);
this.sendBroadcast(mediaScanIntent);
Log.i("debugging",outputFile.getPath().toString());
}
}
public void selectImage(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Log.i("debugging",outputFile.getPath().toString());
intent.setDataAndType(outputFile, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}

How can I handle camera and gallery output simultaneously, without using 2 times onActivityResult?

I have a dialogue, which asks you to choose if to take a picture or to upload one from gallery. The taken/chosen image I set as background on a Button. how can I handle both outputs, as I can't use 2 times onActivityResult?
Here is the method that invokes the camera:
private void invokeCamera() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
And the method that lets you choose from a gallery:
private void openGallery() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
I handle the image received from the camera on the following way:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && data != null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = (Bitmap) extras.get("data");
if (photo != null) {
findViewById(), and whose background you want to update
if (Build.VERSION.SDK_INT > 16) {
imageUploader5.setBackground(new BitmapDrawable(getResources(), photo));
}
} else {
imageUploader5.setBackgroundDrawable(new BitmapDrawable(getResources(), photo));
}
}
}
}
}
First, make a global variable
private final static int GET_PHOTO_BITMAP = 1234;
Then do the following
private void invokeCamera() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, GET_PHOTO_BITMAP);
}
private void openGallery() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, GET_PHOTO_BITMAP);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_PHOTO_BITMAP && data != null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = (Bitmap) extras.get("data");
if (photo != null) {
findViewById(), and whose background you want to update
if (Build.VERSION.SDK_INT > 16) {
imageUploader5.setBackground(new BitmapDrawable(getResources(), photo));
}
} else {
imageUploader5.setBackgroundDrawable(new BitmapDrawable(getResources(), photo));
}
}
}
}
}
Here the key part is request code which is the second parameter of the startActivityForResult(Intent intent, int requestCode)
you can have different requestCodes for different operations and handle it this way in
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case code1:
//task1
break;
case code2:
//task2
break;
//and so on
}
}
but since your case both the callbakcs for startActivityForResult() are supposed to perform the same operation, you can pass same code for both the calls as I have done in the above solution. But make sure that you pass same code when the operations done in the callback are similar.

Unable to capture the snap using intent - Android

I am trying to add a code in my current app so that on one button click ,camera should open and one snap should be taken.
here is my code :
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
PackageManager pm =this.getPackageManager();
final ResolveInfo mInfo = pm.resolveActivity(i, 0);
Intent intent = new Intent();
intent.setComponent(new ComponentName(mInfo.activityInfo.packageName, mInfo.activityInfo.name));
intent.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
}
catch (Exception e){ Log.i(TAG, "Unable to launch camera: " + e); }
Error : it displays -> Complete action uing
Not able to figure it out what i am doin wrong, can any one help me out in this issue.
First
Add permission to your manifest
<uses-feature android:name="android.hardware.camera" android:required="fase" />
Second
static final int REQUEST_IMAGE_CAPTURE = 1;
#Override
protected void onCreate (Bundle savedInstanceState) {
//Your code
//dispatchTakePictureIntent() <-- call this on some button click etc.
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
Third
Retrieve your Snap
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
Retrieved Image is only a small size thumbnail. To get/save full size Image

Camera onActivityResult on fragment

I'm working on some code someone else wrote and I'm having trouble handling the result of a camera intent.
Basically i have a DashBoardActivity which contains a tab with a fragment called "MyProfileContainer", which contains a "SettingsFragment" fragment which contains a "EditProfileFragment"fragment.
In the EditProfileFragment the user can take a picture for his profile. It works but it calls the onActionResult on the Dashboard Activity.
I read some guide on how to redirect it to the EditProfileFragment but I haven't been able to do it.
I'm losing literally days on this one and I can't figure it out.
This is onActivityResult on the Dashboard Activity
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
And this is EditProfileFragment
private Uri imageUri = null;
public void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
getParentFragment().startActivityForResult(intent, 100);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getActivity().getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getActivity().getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
profilePhoto.setImageBitmap(bitmap);
Toast.makeText(getActivity(), selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getActivity(), "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
}
I don't know if I have to override the method on every class between this two or if I'm doing something else wrong, but I'm sure the EditProfileFragment onActivityResult is never called.
I found out the problem, and it was actually a bug of Android.
The fragment that was to recieve the result was a fragment nested inside other fragment and the method call was not properly propagated that deeply.
I had to override the method on the containing fragment manually and it worked.
The easy trick to call OnActivityResult in nested fragment.
1) Add this code in your captureImage method it will start a new activity.
Intent intent = new Intent(getContext(), CameraPreviewActivity.class);
startActivityForResult(intent, 111);
2) Now CameraPreviewActivity activity will open and it will start camera activity and returns the result to fragment.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, FragmentAccPhoto.REQUEST_IMAGE_CAPTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
setResult(RESULT_OK, data);
finish();
}

How to show file chooser when button is clicked?

I am trying to upload image in my app and i want to show some choices when my upload button is clicked. Unfortunately it gives me this error. I dont know what is wrong with the code. I got this from hereTrying open a specific folder in android using intent
thanks in advance!
uploadpic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
openFolder();
}
});
}
public void openFolder()
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/Pictures/");
intent.setDataAndType(uri, "images");
startActivity(Intent.createChooser(intent, "Open folder"));
takephoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
dispatchTakePictureIntent();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
profpic.setImageBitmap(imageBitmap);
}
}
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
You only want the user to select an image, am I right? Then maybe a general file picker isn't whats best suited for your needs.
A quick google search came up with this (also from SO). It seems like that solution is limited to pictures on an SD card.
If you don't want to click the link:
This is how you would start an intent in order to get an image.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
You'll then need to override onActivityResult(), but this is all described in the link.

Categories

Resources