I want to add “remove” option in image chooser int

2019-05-22 12:21发布

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);
       }
   }

2条回答
倾城 Initia
2楼-- · 2019-05-22 13:03

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
    }
}
查看更多
聊天终结者
3楼-- · 2019-05-22 13:06

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/

查看更多
登录 后发表回答