[英]Download image from QuickBlox Server inside BaseAdapter - Android
嗨,我正在使用QuickBlox SDK開發聊天應用程序。 到目前為止,我在這里已經進行了文字聊天。 現在,我正在發送附件(僅適用於圖像)。 首先,我將圖像上傳到quickblox內容部分,成功上傳后,我將使用AsyncTask在BaseAdapter的子類中下載相同的圖像。 這里發生了什么,doInBackground()的return語句沒有執行,所以我在這里想要,我在這里做錯了什么。 請幫幫我 。
這是BaseAdapter子類的代碼片段,我正在其中嘗試從quickblox服務器下載圖像。
BaseAdapter子類的getView()
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
QBChatMessage chatMessage = getItem(position);
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int type = getItemViewType(position) ;
if (convertView == null) {
convertView = vi.inflate(R.layout.list_item_image, parent, false);
holder = createViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
QBUser currentUser = ChatService.getInstance().getCurrentUser();
boolean isOutgoing = chatMessage.getSenderId() == null || chatMessage.getSenderId().equals(currentUser.getId());
setAlignment(holder, isOutgoing);
Collection<QBAttachment> attachments = chatMessage.getAttachments();
//attachments.
if ( attachments != null && attachments.size() > 0) {
String imageid="" ;
for(QBAttachment attachment :attachments){
imageid = attachment.getId();
}
//Here is the AsyncTask where I am trying to download image
new BackgroundOperation(holder ,imageid).execute();
}
return convertView;
}
BaseAdapter子類內的BackgroundOperation
class BackgroundOperation extends AsyncTask<InputStream , Void , InputStream>{
ViewHolder holder ;
int imageid ;
InputStream inputStream;
BackgroundOperation(ViewHolder holder , String imageid){
this.holder = holder ;
this.imageid = Integer.parseInt(imageid);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected InputStream doInBackground(InputStream... params) {
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.post(new Runnable() {
public void run() {
QBContent.downloadFileTask(imageid, new QBEntityCallbackImpl<InputStream>() {
@Override
public void onSuccess(InputStream inputS, Bundle params) {
inputStream = inputS ;
//ImageView img = holder.image_attachment ; //.setImageDrawable(d);
//Toast.makeText(context, "Image download Sucess", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(List<String> errors) {
Log.d("Image Download Error : ", errors.toString());
//Toast.makeText(context, "Image Download Error ", Toast.LENGTH_SHORT).show();
}
}, new QBProgressCallback() {
@Override
public void onProgressUpdate(int progress) {
//Toast.makeText(context, "Image Download Progress ", Toast.LENGTH_SHORT).show();
}
});
}
});
return inputStream;
}
@Override
protected void onPostExecute(InputStream s) {
super.onPostExecute(s);
if(s != null){
Log.d("InputStream Value :", "****************************"+s.toString()+"******************");
Bitmap bmp = BitmapFactory.decodeStream(s);
Drawable d = new BitmapDrawable(context.getResources(), bmp);
if(holder.image_attachment != null)
holder.image_attachment.setImageDrawable(d);
}
}
}
我認為您在異步任務中使用了異步代碼。 顯然,您應該在主UI線程中使用調用QB回調,或者如果您想從后台線程運行此異步代碼,則必須創建一個特殊的處理程序。 參見此處以獲取說明: http : //quickblox.com/developers/Android#Sync_vs_Async
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.