How to cast TransitionDrawable to BitmapDrawable?

218 views Asked by At

I have use PhotoView and Glide libraries for my android project. The app is fetching images from a url and display them in a GridView. When the user clicks on a image it gets loaded into photoview which is inside a fragment of same activity.

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

How to cast it? or Is there any other way to set wallpaper?

1

There are 1 answers

2
Andrew Emad Gabra On
    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