首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >QuickBlox内容,如何从inputStream和低音下载图像到ImageView

QuickBlox内容,如何从inputStream和低音下载图像到ImageView
EN

Stack Overflow用户
提问于 2016-11-10 17:14:04
回答 1查看 197关注 0票数 0

我成功地将一个文件上传到quickblox并成功地下载了它,但是响应是一个InputStream,文档说用输入流下载该文件。这是我的密码。

代码语言:javascript
运行
复制
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) {

        }
    });

}

运行该应用程序时,我会得到以下错误

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-11 11:35:30

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

代码语言:javascript
运行
复制
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) {

        }
    });

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40533427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档