Getting a picture from SD card and Camera

android, android-camera, android-intent, media

Solution

you can have alert dialog to show options. Source code is given below:

AlertDialog.Builder getImageFrom = new AlertDialog.Builder(MainActivity.this);
            getImageFrom.setTitle("Select Image");
            final CharSequence[] opsChars = {"Take Picture", "Open Gallery"};
            getImageFrom.setItems(opsChars, new android.content.DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(which == 0){
                         File file = new File( _path );
                         outputFileUri = Uri.fromFile( file );

                        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        cameraIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);   

                        startActivityForResult(cameraIntent, 7);
                    }else
                        if(which == 1){
                            Intent intent = new Intent();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(Intent.createChooser(intent,
                                "Open Gallery"), 6);
                        }
                    dialog.dismiss();
                }
            });


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 6) {
            Uri selectedImageUri = data.getData();
         pickerImageView.setPadding(0, 0, 0, 0);
         pickerImageView.setScaleType(ScaleType.FIT_XY);
         System.gc();
           String filepath = getPath(selectedImageUri);
           File imagefile = new File(filepath);
           try {
            FileInputStream fis = new FileInputStream(imagefile);
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inPurgeable=true;
            options.inSampleSize =4;
            bi= BitmapFactory.decodeStream(fis,null,options);
            fis.close();
            Bitmap bitmapToRecycle = ((BitmapDrawable)pickerImageView.getDrawable()).getBitmap();  
            bitmapToRecycle.recycle();
            pickerImageView.setImageBitmap(bi); 
            pickerImageView.setPadding(0, 0, 0, 0);
            pickerImageView.setScaleType(ScaleType.FIT_XY);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           pickImageTextView.setText("");
        }
        else if(requestCode == 7){
            Log.i("return", "#####");
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inPurgeable=true;
            options.inSampleSize =4;
            //Bitmap photo = BitmapFactory.decodeFile( _path, options );
             Bitmap photo = (Bitmap) data.getExtras().get("data"); 
             pickerImageView.setImageBitmap(photo); 
             pickerImageView.setPadding(0, 0, 0, 0);
             pickerImageView.setScaleType(ScaleType.FIT_XY);

        } 
    }
}

Problem

I'm currently having a standard image selection (from the SD card) dialog shown when firing off this intent: ``` Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE); ``` This lists all the applications/activities that can return an image (such as Gallery). In this same standard list, I also want to include an option that will start the Camera and return an image taken with it. The problem is I can't figure out how to do this in a non-custom way (building my own dialog with a custom layout, application image + title etc). The Camera activity can be launched like this: ``` Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); URI pictureUri = Uri.fromFile(new File("dummyPath")); camera.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri); ``` Is there an intent that will allow me to select an image from the SD card or one taken with the Camera? Update: I have found a solution, will double check and post it here afterwards.

Original source