Glide 仅在某些设备上返回位图
Glide only returning bitmap on certain devices
我正在使用 Glide 从 URL 中提取位图并将其显示在 ImageView 上,并通过函数(Firebase ML 套件模型)运行 显示它。但是,在某些设备上这不起作用,我相信不会返回有效的位图。我知道它适用的一些设备是 Pixel 3 XL、Pixel 3a 和 Samsung S10e。但是,它不适用于 OnePlus 6 或三星 A5。
在一个 Activity(MainActivity)中,我启动相机应用程序,然后将 url 传递给另一个 Activity(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)
的回调。如果有人对为什么这在某些设备上不起作用有任何想法,我将非常感激。谢谢!
问题实际上是某些设备在相机意图期间破坏了 activity。我遵循了以下答案并且有效:
这是我的代码:
@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();
}
}
我正在使用 Glide 从 URL 中提取位图并将其显示在 ImageView 上,并通过函数(Firebase ML 套件模型)运行 显示它。但是,在某些设备上这不起作用,我相信不会返回有效的位图。我知道它适用的一些设备是 Pixel 3 XL、Pixel 3a 和 Samsung S10e。但是,它不适用于 OnePlus 6 或三星 A5。
在一个 Activity(MainActivity)中,我启动相机应用程序,然后将 url 传递给另一个 Activity(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)
的回调。如果有人对为什么这在某些设备上不起作用有任何想法,我将非常感激。谢谢!
问题实际上是某些设备在相机意图期间破坏了 activity。我遵循了以下答案并且有效:
这是我的代码:
@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();
}
}