here is the camera code
private void selectImage(){ final int Camera_CPTURE = 1; try { Intent capture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(capture,Camera_CPTURE); }catch (ActivityNotFoundException e) { String error ="opps ur devices doesn't support capturing"; Toast toast = Toast.makeText(this,error,Toast.LENGTH_SHORT); toast.show(); } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { picUri = data.getData(); performCrop(); } else if(requestCode == PIC_CROP) { Bundle extra = data.getExtras(); Bitmap thePic = extra.getParcelable("data"); ImageView picView = (ImageView)findViewById(R.id.imageView); picView.setImageBitmap(thePic); } } private void performCrop() { try { Intent cropIntent = new Intent("com.android.camera.action.CROP"); cropIntent.setDataAndType(picUri,"image/*"); cropIntent.putExtra("crop","true"); cropIntent.putExtra("aspectX",1); cropIntent.putExtra("aspectY",1); cropIntent.putExtra("outputX",200); cropIntent.putExtra("outputY",200); cropIntent.putExtra("return-data",true); startActivityForResult(cropIntent,PIC_CROP); }catch (ActivityNotFoundException e) { String error ="opps ur devices doesn't support capturing"; Toast toast = Toast.makeText(this,error,Toast.LENGTH_SHORT); toast.show(); } } @Override public void onClick(View view) { if(view.getId()==R.id.button){ selectImage(); } } }
and the log is here
if(requestCode==Camera_CPTURE && resultCode=RESULT_OK && data!=null) { }
If u get error on Kotlin just mark the data intent as @nullable by adding a ? after the Intent type declaration. So it should look like so:
data: Intent?.
i hope this code usefull..
private String selectedImagePath = ""; final private int PICK_IMAGE = 1; final private int CAPTURE_IMAGE = 2; private String imgPath; private void selectImage() { AlertDialog.Builder builder = new AlertDialog.Builder( ProfileActivity.this); // builder.setTitle("Choose Image Source"); builder.setItems(new CharSequence[] { "Take a Photo", "Choose from Gallery" }, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: Intent intent1 = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); intent1.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri()); startActivityForResult(intent1, CAPTURE_IMAGE); break; case 1: // GET IMAGE FROM THE GALLERY Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult( Intent.createChooser(intent, ""), PICK_IMAGE); break; default: break; } } }); builder.show(); } public Uri setImageUri() { File file = new File(Environment.getExternalStorageDirectory(), "image" + new Date().getTime() + ".png"); Uri imgUri = Uri.fromFile(file); this.imgPath = file.getAbsolutePath(); return imgUri; } public String getImagePath() { return imgPath; }
onActivityForResultSetMethod
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != Activity.RESULT_CANCELED) { if (requestCode == PICK_IMAGE) { selectedImagePath = getAbsolutePath(data.getData()); System.out.println("path" + selectedImagePath); imageView.setImageBitmap(decodeFile(selectedImagePath)); } else if (requestCode == CAPTURE_IMAGE) { selectedImagePath = getImagePath(); System.out.println("path" + selectedImagePath); imageView.setImageBitmap(decodeFile(selectedImagePath)); } else { super.onActivityResult(requestCode, resultCode, data); } } } public Bitmap decodeFile(String path) { try { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, o); // The new size we want to scale to final int REQUIRED_SIZE = 70; // Find the correct scale value. It should be the power of // 2. int scale = 1; while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE) scale *= 2; // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; return BitmapFactory.decodeFile(path, o2); } catch (Throwable e) { e.printStackTrace(); } return null; } public String getAbsolutePath(Uri uri) { String[] projection = { MediaColumns.DATA }; Cursor cursor = managedQuery(uri, projection, null, null, null); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } else return null; }