Back

[SOLVED] Get Image from Storage

  • 1
  • Android
  • Storage
retr122
18 Jun, 2023, 14:35

I'm trying to get Image from my Storage and catch error. How to cast this Object to Image?

Code

TypeScript
  String currentAvatarFileId = (String) "6468811491b6bba36063Avatar";        
  Image currentAccountAvatarImage = (Image) storage.getFilePreview(
  accountsAvatarBucketId,
  currentAvatarFileId,
  new Continuation<Object>() {
                                                        @NotNull
                                                        @Override
                                                        public CoroutineContext getContext() {
                                                            return EmptyCoroutineContext.INSTANCE;
                                                        }

                                                        @Override
                                                        public void resumeWith(@NotNull Object o) {
                                                            try {
                                                                if (o instanceof Result.Failure) {
                                                                    Result.Failure failure =(Result.Failure) o;
                                                                    throw failure.exception;
                                                                } else {
                                                                }
                                                            } catch (Throwable th) {
                                                                Log.e("ERROR", th.toString());
                                                            }
                                                        }
                                                    }
                                            );

Error java.lang.ClassCastException: kotlin.coroutines.intrinsics.CoroutineSingletons cannot be cast to android.media.Image

TL;DR
The user is trying to get an Image from storage and is encountering a ClassCastException when trying to assign the result to an Image variable. The solution is to convert the result, which is a byte array, to an Image. The user mentioned that they converted it to a byte array, which resolved the issue.
Drake
18 Jun, 2023, 15:38

I don't think you can assign it like that. You'd have to assign in the coroutinecallback. Also, I'm pretty sure o would be a byte array so you would need to convert the byte array to image

retr122
18 Jun, 2023, 16:48

Thanks. I convert it to byte array and it works correctly!

Drake
18 Jun, 2023, 16:56

[SOLVED] Get Image from Storage

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more