我正在使用Glide从URL提取位图,并将其显示在ImageView上,然后通过功能(Firebase ML套件模型)运行它。但是,在某些设备上,这是行不通的,并且我相信不会返回有效的位图。我知道可以使用的某些设备是Pixel 3 XL,Pixel 3a和Samsung S10e。但是,它不适用于OnePlus 6或Samsung A5。
在一个活动(MainActivity)中,我启动了相机应用程序,然后将URL传递到另一个活动(PredictionResultActivity)。以下前两个函数将捕获的图像保存到url,第三个函数将url传递给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);
}
}
在PredictionResultActivity的onStart
方法中:
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) {
}
});
在正常工作的设备上,位图正确显示,并且runInterference(resource)
在正常工作。在不工作的设备上,不显示位图,并且永远不会调用runInterference(resource)
的回调。如果有人对为什么它在某些设备上不起作用有任何想法,我将非常感谢。谢谢!
答案 0 :(得分:0)
问题实际上是某些设备在“相机”意图期间破坏了活动的问题。我按照以下答案进行了操作:
https://stackoverflow.com/a/8248392/6075150
这是我的代码:
@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();
}
}