Thankgod  Richard
Thankgod Richard

Reputation: 517

QuickBlox Content, How to download the image from inputStream and bass to ImageView

I successfully uploaded a file to quickblox and download it successfully, But the response is an InputStream, The Documentation said download the file with the input stream. this is my code.

public void downloadProfilePic(int id, final ImageView v, final Context c){
    QBUsers.getUser(id, new QBEntityCallback<QBUser>() {
        @Override
        public void onSuccess(QBUser user, Bundle args) {
            Log.i("propix", String.valueOf(user.getFileId()));
            QBContent.downloadFileById(user.getFileId(), new QBEntityCallback<InputStream>(){
                @Override
                public void onSuccess(InputStream inputStream, Bundle params) {
                    Bitmap bmp = BitmapFactory.decodeStream(inputStream);
                    v.setImageBitmap(bmp);
                }

                @Override
                public void onError(QBResponseException errors) {
                    Log.i("propic",errors.getMessage());
                    v.setImageResource(R.mipmap.profile_img);
                }
            }, new QBProgressCallback() {
                @Override
                public void onProgressUpdate(int progress) {

                }
            });


        }

        @Override
        public void onError(QBResponseException errors) {

        }
    });

}

Running the app I get the following error

11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err: android.os.NetworkOnMainThreadException
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:657)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.android.okio.Okio$2.read(Okio.java:113)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.android.okio.RealBufferedSource.read(RealBufferedSource.java:48)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:442)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.android.okio.RealBufferedSource$1.read(RealBufferedSource.java:168)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.quickblox.core.request.ProgressHttpEntityWrapper$ProgressFilterInputStream.read(ProgressHttpEntityWrapper.java:96)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:649)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.shixxels.thankgodrichard.spotpopfinal.Helpers$27$1$override.onSuccess(Helpers.java:849)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.shixxels.thankgodrichard.spotpopfinal.Helpers$27$1$override.access$dispatch(Helpers.java)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.shixxels.thankgodrichard.spotpopfinal.Helpers$27$1.onSuccess(Helpers.java:0)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.shixxels.thankgodrichard.spotpopfinal.Helpers$27$1.onSuccess(Helpers.java:845)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.quickblox.core.Query.notifySuccess(Query.java:372)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.quickblox.core.Query$VersionEntityCallback.completedWithResponse(Query.java:404)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.quickblox.core.Query.completedWithResponse(Query.java:277)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.quickblox.core.server.HttpRequestRunnable$1.handleMessage(HttpRequestRunnable.java:42)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at android.os.Looper.loop(Looper.java:135)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5221)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal D/skia: ---- read threw an exception
11-10 12:11:36.889 32360-32360/com.shixxels.thankgodrichard.spotpopfinal D/skia: --- SkImageDecoder::Factory returned null

Upvotes: 0

Views: 206

Answers (1)

Thankgod  Richard
Thankgod Richard

Reputation: 517

I found out the error was because i was trying to download the picture from the main thread. this is the code that works.

public void downloadProfilePic(int id, final ImageView v, final Context c, final Activity ac){
    QBUsers.getUser(id, new QBEntityCallback<QBUser>() {
        @Override
        public void onSuccess(QBUser user, Bundle args) {
            Log.i("propix", String.valueOf(user.getFileId()));
            QBContent.downloadFileById(user.getFileId(), new QBEntityCallback<InputStream>(){

                @Override
                public void onSuccess(final InputStream inputStream, Bundle params) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            final Bitmap bmp = BitmapFactory.decodeStream(inputStream);
                            ac.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    v.setImageBitmap(bmp);
                                }
                            });
                        }
                    }).start();



                }

                @Override
                public void onError(QBResponseException errors) {
                    Log.i("propic",errors.getMessage());
                    v.setImageResource(R.mipmap.profile_img);
                }
            }, new QBProgressCallback() {
                @Override
                public void onProgressUpdate(int progress) {

                }
            });


        }

        @Override
        public void onError(QBResponseException errors) {

        }
    });

}

Upvotes: 1

Related Questions