java.lang.ClassCastException:android.graphics.drawable.LayerDrawable无法转换为android.graphics.drawable.BitmapDrawable

时间:2019-06-19 06:20:31

标签: android

我遇到了三星Galaxy S9的ClassCast异常。我已经在其他运行Android 9.0的诺基亚,运行8.1的摩托罗拉的手机上尝试了相同的代码,并且运行良好。

请帮助。

private File createImageFile() {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = this.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = null;
    if (mBrandLogo.getDrawable() != null) {
        Bitmap bitmap = ((BitmapDrawable) mBrandLogo.getDrawable()).getBitmap();
        try {
            image = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",         /* suffix */
                    storageDir      /* directory */
            );
            FileOutputStream out = new FileOutputStream(image);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Save a file: path for use with ACTION_VIEW intents
        //mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    }
    return image;
}

0 个答案:

没有答案