0

I am using Glide to extract a Bitmap from a URL and display it on an ImageView and run it through a function (Firebase ML kit model). However, on some devices this does not work, and I believe a valid bitmap is not being returned. Some devices I know it works on are the Pixel 3 XL, Pixel 3a, and Samsung S10e. It does not, however, work on the OnePlus 6 nor the Samsung A5.

In one Activity (MainActivity), I launch the camera app then pass along the url to a different Activity (PredictionResultActivity). The first two of the following functions save a captured image to a url, and the third function passes the url along to a PredictionResultActivity:

// Function to call when diagnose plant button clicked
    public void capturePlant(View view) {
        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
            // TODO: error handling
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.sproutleaf.android.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

    // Create image file of captured plant
    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
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }

// On result of activity launched from intent
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // If requestCode = camera app launched
        if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
            // Send path as intent
            Intent intent = new Intent(this, PredictionResultActivity.class);
            intent.putExtra("capturedPhotoPath", mCurrentPhotoPath);
            startActivity(intent);
        }
    }

And in PredictionResultActivity's onStart method:

 Intent intent = getIntent();
        mCapturedPhotoPath = intent.getStringExtra("capturedPhotoPath");
        Glide.with(this)
                .asBitmap()
                .load(mCapturedPhotoPath)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        mCapturedImageView.setImageBitmap(resource);
                        try {
                            runInference(resource);
                        } catch (FirebaseMLException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                    }
                });

On the devices that work, the bitmap is properly displayed and runInterference(resource) works properly. On the devices that don't work, the bitmap is not displayed and a callback from runInterference(resource) is never called. If anybody has any ideas on why this does not work on some devices I would appreciate it very much. Thank you!

Russell C.
  • 588
  • 6
  • 25
  • It must be giving some kind of exception. Did you check that on catch block?? – ॐ Rakesh Kumar Aug 22 '19 at 05:11
  • @RakeshKumar the thing is I don't have those devices on hand - they are just my friends trying it out so I can't check the logs – Russell C. Aug 22 '19 at 05:12
  • Either, Create emulator with same specs or put the toast on catch block then try that build in your friend device – ॐ Rakesh Kumar Aug 22 '19 at 05:17
  • i think it should be something like a cleartext issue, check the logs.... it should have an exception – Kushan Aug 22 '19 at 05:27
  • If you were curious, the problem was actually a matter of some devices destroying the activity during the Camera intent. The explanation is in my answer. Thank you guys for your help though! Appreciate it – Russell C. Aug 22 '19 at 06:55

1 Answers1

0

The problem was actually a matter of some devices destroying the activity during the Camera intent. I followed the following answer and it worked:

https://stackoverflow.com/a/8248392/6075150

Here is my code:

 @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mCurrentPhotoPath != null) {
            outState.putString("cameraImageUri", mCurrentPhotoPath);
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState.containsKey("cameraImageUri")) {
            mCurrentPhotoPath = Uri.parse(savedInstanceState.getString("cameraImageUri")).toString();
        }
    }
Russell C.
  • 588
  • 6
  • 25