Vikas Sharma
Vikas Sharma

Reputation: 35

why is CATEGORY_OPENABLE used for in android

Here I have written the code for image picker from gallery, but can anyone tell me what is the roll of setAction() and addCategory() ?

There are a lots of "static final String" available in Intent Class file, I am totally unaware of using these ACTION and CATEGORY parameters in my program

public class ImagePicker extends BaseActivity implements View.OnClickListener {

  private final int PICK_FROM_GALLERY_REQUEST = 1;
  private ImageView pickedImage;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.onStart();
    setContentView(R.layout.activity_image_picker);
    pickedImage= (ImageView) findViewById(R.id.image);
    Button cameraButton= (Button) findViewById(R.id.pick_from_camera);
    Button galleryButton= (Button) findViewById(R.id.pick_from_gallery);
    cameraButton.setOnClickListener(this);
    galleryButton.setOnClickListener(this);
    setViewHeight(pickedImage);
  }

  private void setViewHeight(ImageView pickedImage) {
    DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
    pickedImage.getLayoutParams().height=displayMetrics.heightPixels/2;
  }


  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==PICK_FROM_GALLERY_REQUEST && resultCode==RESULT_OK && data!=null){
        InputStream stream = null;
        try {
            stream = getContentResolver().openInputStream(
                    data.getData());
            Bitmap bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
            pickedImage.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()){
        case R.id.pick_from_gallery:
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(intent, PICK_FROM_GALLERY_REQUEST);
            break;
        case R.id.pick_from_camera:

            break;
    }
  }

Upvotes: 3

Views: 5955

Answers (1)

Lucius Pertis
Lucius Pertis

Reputation: 76

Intent.setAction is used to determine what kind of action do you want the intent receiving application to do. In your case you want to get some data, specifically an image; so you say this to Android by using setAction() and passing the argument as ACTION_GET_CONTENT. For instance, if you wanted to send an image rather than receive one, you would have passed ACTION_SEND, ACTION_SENDTO or ACTION_SEND_MULTIPLE. Intent.ACTION simply gives you a chance to specify the function or action that the intent receiving app must be capable to do according to your needs.

Intent.CATEGORY is another specification you can add to your intent. In your case OPENABLE denotes that the data you want to receive can be opened as a File object i.e. with read and write permissions and have complete access to the physical location of the data(in your case the image file). Remember that this means you will need to request the storage-permissions before hand and this category will cause a raise in your securities. If you just need to display the image and do not need to manipulate the image file physically, DON'T USE this category. If you want to manipulate the image and store it SEPARATELY consider making a separate copy of the image and manipulate that copy in your own ExternalStorage provided by Android.

Upvotes: 6

Related Questions