Open camera using

I want to use the intention to open the camera in android

I used the following code, but when I click the button (the action of which acts onclick (), the applications close on themselves

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 anyone can help please give me the full code

+6
source share
6 answers
 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 the purpose // ......................... 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); } 

// ............ intention .........

  // .................. 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; } } 
+8
source

try this code

 public static final int CAMERA_PIC_REQUEST = 1;//firstly define this Intent photo= new Intent("android.media.action.IMAGE_CAPTURE"); startActivityForResult(photo, CAMERA_PIC_REQUEST); 
+3
source

The parameter passed to the on-click method is of type View not int , as you show.

According to this tutorial, your onClick method should be as follows:

 public void onclick( View v ){ Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(takePictureIntent, CAMERA_PIC_REQUEST); } 

Where CAMERA_PIC_REQUEST defined as (although I'm not quite sure why you need to statically hard-code this value in your application):

 private static final int CAMERA_PIC_REQUEST = 1337; 


Update

CAMERA_PIC_REQUEST used to uniquely identify the return result on onActivityResult . Multiple startActivityForResult requests can be startActivityForResult at a time.

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_PIC_REQUEST) { if (resultCode == RESULT_OK) { tv.setText("Got picture!"); imageData = (Bitmap) data.getExtras().get("data"); ImageView image = (ImageView) findViewById(R.id.imageView1); image.setImageBitmap(imageData); } else if (resultCode == RESULT_CANCELED){ tv.setText("Cancelled"); } } } 
+2
source

I know this question is old and answered, but for those who ask how to get the image file? this decision.

 String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath(); String targetPath = ExternalStorageDirectoryPath + "/DCIM/Camera"; File targetDirector = new File(targetPath); File[] files = targetDirector.listFiles() String ImagePath = files[ file.files-1 ].getAbsolutePath(); Bitmap bmp = BitmapFactory.decodeFile(pathName); ivImage.setImageBitmap( bmp ); 

Also, you do not need to add camera permissions to the manifest file.

-Call and vote if this helps you

-Happy Codings ..

+1
source

It’s very easy to launch the camera from your Android application: just write the code of two lines in the onClick method

 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, CAMERA_PIC_REQUEST ); 

Add one constant field to your class (you can use any random number instead of 7)

 private int CAMERA_PIC_REQUEST = 7; 
+1
source

Please take a look at this code, this works 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 applies this method to read the image when you shot the image from the 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); } 
0
source

Source: https://habr.com/ru/post/946037/


All Articles