Tuesday, 5 June 2018

Code To Get Image From Gallery and From Camera In Android

  No comments
Get Image From Gallery
public void openGallary(View view) {
   Intent intent = new Intent(Intent.ACTION_PICK);
   intent.setType("image/*");
   startActivityForResult(intent, 3);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   file = null;
   Log.e(TAG, "onActivityResult: ");
  if (requestCode == 3 & resultCode == RESULT_OK) {
       Toast.makeText(this, "Image Selected from Gallery", Toast.LENGTH_SHORT).show();
       Uri pickedImage = data.getData();
       imageview.setImageURI(pickedImage);

       file = persistImage(getBitmapFromURI(pickedImage), "image2");
   }
}
Get Image From Camera
public void openCamera(View view) {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
   File photo = new File(Environment.getExternalStorageDirectory(), "image1");
   intent.putExtra(MediaStore.EXTRA_OUTPUT,
           Uri.fromFile(photo));
   imageUri = Uri.fromFile(photo);
   startActivityForResult(intent, 2);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   Log.e(TAG, "onActivityResult: ");
   if (requestCode == 2 && resultCode == RESULT_OK) {
       Log.e(TAG, "onActivityResult: call");
      
       try {
           ImageView imageview = (ImageView) findViewById(R.id.capture_img); /           selectedImage = imageUri;
           imageview.setImageURI(imageUri);
       } catch (Exception e) {
           Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                   .show();

       }
   }
}



No comments :

Post a Comment

Loading...