如何在Bitmap的Array列表中存储从服务器检索到的图像? 我试过但arraylist显示空值。
以下代码从服务器获取图片: -
ParseQuery<ParseObject> imgquery = new ParseQuery<ParseObject>("ChatRecords");
imgquery.getInBackground(ChatRecordObjectID, new GetCallback<ParseObject>()
{
@Override
public void done(ParseObject object,ParseException e)
{
System.out.println("in 2nd done:"+time2);
ParseFile fileObject = (ParseFile) object.get("ImageMessage");
fileObject.getDataInBackground(new GetDataCallback()
{
@Override
public void done(byte[] data, ParseException e)
{
if (e == null)
{
bmp= BitmapFactory.decodeByteArray(data, 0,data.length);
imageBitmap.add(bmp);
}
else
{
Log.d("test","There was a problem downloading the data."); }
}
});
}
});
Following Code to retrieve image in array list :-
for(int i=0;i<imageBitmap.size();i++)
{
ImageView imageView = new ImageView(CurrentUserActivity.this);
imageView.setBackgroundResource(R.drawable.msgrecv);
imageView.setAdjustViewBounds(true);
imageView.setMaxHeight(150);
imageView.setMaxWidth(inScrollViewLeft.getLayoutParams().width);
imageView.requestLayout();
imageView.setImageBitmap(imageBitmap.get(i));
inScrollViewLeft.addView(imageView);
}
错误是:
04-28 09:57:08.280: E/AndroidRuntime(2769): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-28 09:57:08.280: E/AndroidRuntime(2769): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
答案 0 :(得分:0)
将您的位图对象添加到ArratList
,例如
ArrayList<Bitmap> images = new ArrayList<>();
images.add(downlaodedBitmap);
答案 1 :(得分:0)
我在上面的代码中进行了以下更改,有时图像是按顺序设置的,有时不是。请检查代码并让我知道更改:
public void searchChatRecord()
{
k=0;
ParseQuery<ParseObject> pq = ParseQuery.getQuery("ChatRecords");
pq.whereNotEqualTo("objectId", null);
pq.orderByAscending("createdAt");
pq.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> chatObjects, ParseException e) {
if(e == null)
{
if(chatObjects.size()>0)
{
System.out.println("chatObjects.size() : "+chatObjects.size());
for(int i =0;i<chatObjects.size();i++)
{
k++;
if(chatObjects.get(i).getString("SenderObjectID").equals(currentUser.getObjectId()) && chatObjects.get(i).getString("RecieverObjectID").equals(recieverObjectID))
{
addImg.add("S2R");
ChatRecordObjectID = chatObjects.get(i).getObjectId();
chatID.add(ChatRecordObjectID);
time1=chatObjects.get(i).getString("date_time"); // ..po.getString("date_time");
System.out.println("in 1st :"+time1);
time.add(time1);
}
else
if(chatObjects.get(i).getString("SenderObjectID").equals(recieverObjectID) && chatObjects.get(i).getString("RecieverObjectID").equals(currentUser.getObjectId()))
{
addImg.add("R2S");
ChatRecordObjectID =chatObjects.get(i).getObjectId();
chatID.add(ChatRecordObjectID);
time2= chatObjects.get(i).getString("date_time");
time.add(time2);
System.out.println("in 2nd :"+time2);
}
if(k==chatObjects.size())
{
System.out.println(k);
for(int j=0;j<addImg.size();j++)
{
if(addImg.get(j).equals("S2R"))
{
userToReciever(chatID.get(j), time.get(j));
}
else
if(addImg.get(j).equals("R2S"))
{
recieverToUser(chatID.get(j), time.get(j));
}
}
}
}
}
else
{
Toast.makeText(getApplicationContext(), "No chat records", Toast.LENGTH_SHORT).show();
}
}
}
});
}
public void userToReciever(String id,final String msgTime)
{
ParseQuery<ParseObject> imgquery = new ParseQuery<ParseObject>("ChatRecords");
imgquery.getInBackground(id, new GetCallback<ParseObject>()
{
@Override
public void done(ParseObject object,ParseException e)
{
System.out.println("in 1st done:"+msgTime);
ParseFile fileObject = (ParseFile) object.get("ImageMessage");
fileObject.getDataInBackground(new GetDataCallback()
{
@Override
public void done(byte[] data, ParseException e)
{
if (e == null)
{
Log.d("test","We've got data in data.");
bmp= BitmapFactory.decodeByteArray(data, 0,data.length);
TextView t = new TextView(CurrentUserActivity.this);
t.setPadding(10,50, 0, 2);
t.setText("me"+" "+msgTime);
ImageView imageView = new ImageView(CurrentUserActivity.this);
imageView.setBackgroundResource(R.drawable.shape);
imageView.setAdjustViewBounds(true);
imageView.setMaxHeight(150);
imageView.setMaxWidth(inScrollViewLeft.getLayoutParams().width);
imageView.requestLayout();
imageView.setImageBitmap(bmp);
inScrollViewLeft.addView(t);
inScrollViewLeft.addView(imageView);
}
else
{
Log.d("test","There was a problem downloading the data.");
}
}
});
}
});
}
public void recieverToUser(String id,final String msgTime) {
ParseQuery<ParseObject> imgquery = new ParseQuery<ParseObject>("ChatRecords");
imgquery.getInBackground(id, new GetCallback<ParseObject>()
{
@Override
public void done(ParseObject object,ParseException e)
{
System.out.println("in 2nd done:"+msgTime);
ParseFile fileObject = (ParseFile) object.get("ImageMessage");
fileObject.getDataInBackground(new GetDataCallback()
{
@Override
public void done(byte[] data, ParseException e)
{
if (e == null)
{
Log.d("test","We've got data in data.");
bmp= BitmapFactory.decodeByteArray(data, 0,data.length);
TextView t = new TextView(CurrentUserActivity.this);
t.setPadding(10,50, 0, 2);
t.setText("me"+" "+msgTime);
ImageView imageView = new ImageView(CurrentUserActivity.this);
imageView.setBackgroundResource(R.drawable.msgrecv);
imageView.setAdjustViewBounds(true);
imageView.setMaxHeight(150);
imageView.setMaxWidth(inScrollViewLeft.getLayoutParams().width);
imageView.requestLayout();
imageView.setImageBitmap(bmp);
inScrollViewLeft.addView(t);
inScrollViewLeft.addView(imageView);
}
else
{
Log.d("test","There was a problem downloading the data.");
}
}
});
}
});
}