Treewallie
Treewallie

Reputation: 356

How can I send an image from an imageview, after taking a picture, to pc via bluetooth?

  1. I take photo
  2. Photo gets stored in ImageView
  3. I want to be able to send that photo stored in the ImageView to my pc via Bluetooth when user selects Send To Pharmacy button.

Is this possible? Since the image is constantly changing, how would I do this? I have looked up some tutorials and StackOverflow questions but they all seem to be focused on images in a drawable folder already.

But in this case, the image is different every time the user takes a photo. I just want to know how I can send the current photo that is in the ImageView, and send it to a PC via Bluetooth. Thanks for any help I appreciate this.

    package com.cognizant.expressprescriptionregistration;


    import static android.os.Environment.getExternalStoragePublicDirectory;

    public class Register extends AppCompatActivity {

        Button bPicButton;
        ImageView imageView;
        String pathToFile;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register);

            bPicButton = findViewById(R.id.bTakePhoto);
            imageView = findViewById(R.id.imagePrescription);

            // Ask for permission for Camera and Storage
            if (Build.VERSION.SDK_INT >= 23){
                requestPermissions(new String[] {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
            }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            // if camera successfully pops up and takes photo, set photo in ImageView
            if (resultCode == RESULT_OK){
                if (requestCode == 1){
                    Bitmap bitmap = BitmapFactory.decodeFile(pathToFile);
                    imageView.setImageBitmap(bitmap);
                }
            }
        }

        // Take Picture button onClick listener
        public void takePhoto(View view) {

            setPhotoTaken();

        }

        private void setPhotoTaken() {

            Intent takePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            // Make sure the app can handle our intent
            if (takePhoto.resolveActivity(getPackageManager()) != null) {

                File photoFile = null;

                // Created Photo file
                photoFile = createPhotoFile();

                // Get path of our photo file
                if (photoFile != null) {

                    pathToFile = photoFile.getAbsolutePath();
                    Uri photoUri = FileProvider.getUriForFile(Register.this, "com.cognizant.expressprescriptionregistration.fileprovider", photoFile);
                    takePhoto.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
                    startActivityForResult(takePhoto, 1);
                }


            }

        }

        // Create the file where the photo will be stored
        private File createPhotoFile() {

            // Name of file
            String name = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

            // Location of storage
            File storedDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            File photo = null;

            try {

                // Creates file in storedDir
                photo = File.createTempFile(name, ".jpg", storedDir);
            } catch (IOException e) {
                e.printStackTrace();
            }

            return photo;
        }

        // Send to Pharmacy Button
        public void sendToPharmacy(View view) {

        }

    }

Upvotes: 0

Views: 27

Answers (1)

Yevhenii
Yevhenii

Reputation: 840

You can get current image from ImageView by getting the bitmap from ImageView:

Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();

After this you can save and share the bitmap.

Upvotes: 1

Related Questions