简体   繁体   English

如何将TransitionDrawable转换为BitmapDrawable?

[英]How to cast TransitionDrawable to BitmapDrawable?

I have use PhotoView and Glide libraries for my android project. 我已经为我的android项目使用了PhotoViewGlide库。 The app is fetching images from a url and display them in a GridView. 该应用程序正在从URL提取图像并将其显示在GridView中。 When the user clicks on a image it gets loaded into photoview which is inside a fragment of same activity. 当用户单击图像时,它将被加载到同一活动片段内的photoview中。

Now i want to set that image as a wallpaper so I have written the following inside the button click listener: 现在,我要将图像设置为墙纸,因此我在按钮单击侦听器中编写了以下内容:

WallpaperManager wallpaperManager = WallpaperManager.getInstance(getActivity());
bitmap = ((BitmapDrawable) photoView.getDrawable()).getBitmap();
try{
    wallpaperManager.setBitmap(bitmap);
    Toast.makeText(getActivity(), "wallpaper set", Toast.LENGTH_SHORT).show();
}catch(IOException e){
     e.printStackTrace();
}

The error is in the following line: 错误在以下行中:

bitmap = ((BitmapDrawable) photoView.getDrawable()).getBitmap();

Error is: Cannot cast TransitionDrawable into BitmapDrawable 错误是:无法将TransitionDrawable转换为BitmapDrawable

How to cast it? 如何铸造? or Is there any other way to set wallpaper? 或还有其他设置墙纸的方法吗?

    ImageVIEW.setImageBitmap( getUri( Uri.parse( blob ) ) );


private Bitmap getUri(Uri uri) {
    ParcelFileDescriptor parcelFileDescriptor = null;
    try {
        parcelFileDescriptor = activity.getContentResolver().openFileDescriptor( uri, "r" );
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
        Bitmap image = BitmapFactory.decodeFileDescriptor( fileDescriptor );
        parcelFileDescriptor.close();
        return image;
    } catch (Exception e) {
        Log.e( TAG, "Failed to load image.", e );
        return null;
    } finally {
        try {
            if (parcelFileDescriptor != null) {
                parcelFileDescriptor.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e( TAG, "Error closing ParcelFile Descriptor" );
        }
    }

all details in this link 此链接中的所有详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM