我成功地将一个文件上传到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$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
发布于 2016-11-11 11:35:30
我发现错误是因为我试图从主线程下载图片。这是起作用的代码。
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) {
}
});
}
https://stackoverflow.com/questions/40533427
复制相似问题