Search code examples
androidandroid-intentandroid-camera

open camera using intent


I want to use intent to open camera in Android .

I used the following code but when i press the button (whose action is onclick() function the app closes on itself .

 public void onclick(int actionCode){
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
     startActivityForResult(takePictureIntent, actionCode);
} 
public static boolean isIntentAvailable(Context context, String action) { 
    final PackageManager packageManager = context.getPackageManager(); 
    final Intent intent = new Intent(action); 
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); 
    return list.size() > 0;
}

If someone can help me .


Solution

  • File destination;
    Uri selectedImage;
    public static String selectedPath1 = "NONE";
    private static final int PICK_Camera_IMAGE = 2;
    private static final int SELECT_FILE1 = 1;
    public static Bitmap bmpScale;
    public static String imagePath;
    
    
    
        mcamera = (Button) findViewById(R.id.button1);
        mcamera.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                String name = dateToString(new Date(), "yyyy-MM-dd-hh-mm-ss");
                destination = new File(Environment
                        .getExternalStorageDirectory(), name + ".jpg");
    
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(destination));
                startActivityForResult(intent, PICK_Camera_IMAGE);
    
            }
        });
    
        // ......................gallery_function..........//
        mgallery = (Button) findViewById(R.id.button2);
        mgallery.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                openGallery(SELECT_FILE1);
            }
        });
    

    for intent // .........................Gallery function.................//

        public void openGallery(int SELECT_FILE1) {
    
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(
                Intent.createChooser(intent, "Select file to upload "),
                SELECT_FILE1);
    
    }
    

    //............ intent .........

                    // ..................
    protected void onActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
        Uri selectedImageUri = null;
        String filePath = null;
        switch (requestCode) {
        case SELECT_FILE1:
            if (resultCode == Activity.RESULT_OK) {
                selectedImage = imageReturnedIntent.getData();
    
                if (requestCode == SELECT_FILE1) {
                    selectedPath1 = getPath(selectedImage);
                    // mimagepath.setText(selectedPath1);
                    // Toast.makeText(Camera.this, "" + selectedPath1 + "",
                    // 500).show();
    
                    if (selectedPath1 != null) {
    
                        BitmapFactory.Options options = new BitmapFactory.Options();
    
                        options.inJustDecodeBounds = true;
                        // image path `String` where your image is located
                        BitmapFactory.decodeFile(selectedPath1, options);
    
    
    
                    // Log.d("setpath ", "setpath " + selectedPath1);
                    ;
    
                }
            }
    
            break;
        case PICK_Camera_IMAGE:
            if (resultCode == RESULT_OK) {
    
                try {
                    in = new FileInputStream(destination);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 4;
                imagePath = destination.getAbsolutePath();
    
                // Toast.makeText(Camera.this, "" + imagePath +
                // "",Toast.LENGTH_LONG).show();
    
            break;
    
        }
    
    }