QuickBlox 内容,如何将图像从 inputStream 和 bass 下载到 ImageView

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

我成功将文件上传到 quickblox 并成功下载,但响应是 InputStream,文档说使用输入流下载文件。这是我的代码。

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) {

        }
    });

}

运行 我收到以下错误的应用程序

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.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.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$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$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.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.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.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

我发现错误是因为我试图从主线程下载图片。这是有效的代码。

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) {

        }
    });

}