android – GetBitmap() returns null and Intent data is empty-ThrowExceptions

Exception or error:

I’ve been trying to create an app that captures an image from the camera using the camera intent and send it onto a server for further analysis. For some reason, the getBitmap(), BitmapFactory.decodeFile() return null everytime I pass the uri to them. My intent data is also empty when the onActivityResult is called. What should I do? Please help.

private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                Toast.makeText(this, "Error creating image: " + ex.getMessage(), Toast.LENGTH_LONG).show();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.android.FileProvider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            //File image1 = createImageFile();
            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && data != null) {
                //Uri filePath = Uri.parse(currentPhotoPath);
                try {
                    File file = new File(currentPhotoPath);
                    //Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                    Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath);
                    /*Bundle extras = data.getExtras();
                    Bitmap imageBitmap = (Bitmap) extras.get("data");
                    Bitmap lastBitmap = imageBitmap;*/
                    //Bitmap bitmap = MediaStore.Images.Media.getBitmap(HomeActivity.this.getContentResolver(), Uri.fromFile(file));
                    //encoding image to string
                    String image = getStringImage(bitmap);
                    Log.d("image", image);
                    //passing the image to volley
                    sendImage(image);

                } catch (OutOfMemoryError e) {
                    e.printStackTrace();
                }
            }
        }
        catch (Exception e){
            Toast.makeText(HomeActivity.this, "IOException: " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *