Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 .

like image 623
user2429689 Avatar asked May 28 '13 19:05

user2429689


2 Answers

please have a look at this code, this is working fine with me

//define the file-name to save photo taken by Camera activity
fileName = "new-photo-name.jpg";
//create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

also apply this method to read image when you taken the image from camera.

    //handling intent responses
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK)
      try {
        if (bitmap != null) {bitmap.recycle();}

        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(imageUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        imageUriString = cursor.getString(column_index);

        getContentResolver().notifyChange(imageUri, null);
        ContentResolver cr = getContentResolver();

        try {
            bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, imageUri);
            imageButtonPictureShop.setImageBitmap(bitmap);

// this.uploadImage(); this.executeMultipartPost(); // this.uploadFile(imageUriString); } catch (Exception e) { Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show(); if(e.getMessage() != null)Log.e("Exception" , e.getMessage()); else Log.e("Exception" , "Exception"); e.printStackTrace(); }

      } 
    catch (Exception e) 
    {
        if(e.getMessage() != null)Log.e("Exception"  , e.getMessage());
        else Log.e("Exception"  , "Exception");
        e.printStackTrace();
    }

    super.onActivityResult(requestCode, resultCode, data);
  }
like image 79
Syed_Adeel Avatar answered Oct 26 '22 01:10

Syed_Adeel


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;

    }

}
like image 42
Rishi Gautam Avatar answered Oct 25 '22 23:10

Rishi Gautam