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!