【问题标题】:Glide only returning bitmap on certain devicesGlide 仅在某些设备上返回位图
【发布时间】:2019-12-27 08:44:05
【问题描述】:

我正在使用 Glide 从 URL 中提取位图并将其显示在 ImageView 上并通过函数(Firebase ML 套件模型)运行它。但是,在某些设备上这不起作用,我相信没有返回有效的位图。我知道它适用的一些设备是 Pixel 3 XL、Pixel 3a 和三星 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) 的回调。如果有人对为什么这在某些设备上不起作用有任何想法,我将非常感激。谢谢!

【问题讨论】:

  • 它必须给出某种异常。你在 catch 块上检查了吗??
  • @RakeshKumar 问题是我手头没有这些设备 - 他们只是我的朋友在试用,所以我无法查看日志
  • 要么,创建具有相同规格的模拟器,要么将 toast 放在 catch 块上,然后尝试在您的朋友设备中构建
  • 我认为应该是明文问题,检查日志....应该有异常
  • 如果您好奇,问题实际上是一些设备在相机意图期间破坏了活动。解释在我的回答中。谢谢你们的帮助!欣赏它

标签: android android-intent bitmap android-camera android-glide


【解决方案1】:

问题实际上是一些设备在相机意图期间破坏活动的问题。我按照以下答案进行了操作:

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();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多