gpt4 book ai didi

java - 使用 ArrayList 通过 ParseFile 下载多个图像

转载 作者:太空宇宙 更新时间:2023-11-04 10:23:56 24 4
gpt4 key购买 nike

我无法在全局数组列表中存储图像位图。我已经声明了一个全局数组列表最终 ArrayList prod_image = new ArrayList();我正在尝试从 Parse Server 调用图像并将其保存在数组列表中。在 ParseFile 回调中,我试图保存位图, prod_image.add(bp);但是,该数组在回调之外返回空值。

如何在回调中将图像存储在数组中?

ParseFile file = (ParseFile) object.get("image_book");

file.getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] data, ParseException e) {
if(e== null && data != null){

Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

if(bitmap != null){
Log.i("image download","Success");

bp = bitmap;
Log.i("image Content",bp.toString());

prod_image.add(bp);
//adapter.notifyDataSetChanged();
if(prod_image.size()>0){
Log.i("inside count","Ok");
}else {
Log.i("inside Count","Not Ok");
}


//adapter.notifyDataSetChanged();

}else {
Log.i("image download","Failure");
}
}else {
e.printStackTrace();
}
}
});


`
public class UserFeed extends AppCompatActivity {

ImageView account;
ImageView notif;
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
//UserFeedAdapter adapter;
ArrayList<String> prod_name ;
ArrayList<String> prod_cat;
ArrayList<String> prod_pub;
ArrayList<String> prod_price;

Bitmap bp=null;
RecyclerView.LayoutManager layoutManager;
final ArrayList<Bitmap> prod_image = new ArrayList<Bitmap>();
//final Bitmap bitmap;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_feed);
//setTitle("My Feed");

account = (ImageView)findViewById(R.id.account);
notif = (ImageView)findViewById(R.id.notif);

account.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), AccountActivity.class);
startActivity(intent);
}
});

notif.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), NotificationActivity.class);
startActivity(intent);
}
});

//myListView = (ListView)findViewById(R.id.listView);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);

layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);


prod_name = new ArrayList<>();
prod_cat = new ArrayList<>();
prod_pub = new ArrayList<>();
prod_price = new ArrayList<>();


//MyData myImage = new MyData();

ParseQuery<ParseObject> dashboardQuery = new ParseQuery<ParseObject>("Sell");
dashboardQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if(e == null){
if(objects.size() > 0){
for (final ParseObject object : objects){
String name = (String) object.get("name");
String cat = (String) object.get("category");
String pub = (String)object.get("publisher");
String price = (String) object.get("price");
//Bitmap image = (Bitmap)object.get("image_book");

prod_name.add(name);
prod_cat.add(cat);
prod_pub.add(pub);
prod_price.add(price);


//prod_image.add(image);

ParseFile file = (ParseFile) object.get("image_book");

file.getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] data, ParseException e) {
if(e== null && data != null){

Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

if(bitmap != null){
Log.i("image download","Success");

bp = bitmap;
Log.i("image Content",bp.toString());

prod_image.add(bp);
//adapter.notifyDataSetChanged();
if(prod_image.size()>0){
Log.i("inside count","Ok");
}else {
Log.i("inside Count","Not Ok");
}


//adapter.notifyDataSetChanged();

}else {
Log.i("image download","Failure");
}
}else {
e.printStackTrace();
}
}
});

//prod_image.add(bp);
//<-- not ok
//adapter.notifyDataSetChanged();

}

}
}
if(prod_image.size()>0){
Log.i("outside count","Ok");
}else {
Log.i("outside Count","Not Ok");
}
//<-- It's first crashing then second time its loading successfully!!
adapter = new UserFeedAdapter(prod_name,prod_cat,prod_pub,prod_price,prod_image);
//recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
}
});


//<---Not working at all
//adapter = new UserFeedAdapter(prod_name,prod_cat,prod_pub,prod_price,prod_image);
//recyclerView.setAdapter(adapter);
if(prod_image.size()>0){
Log.i("very outside count","Ok");
}else {
Log.i("very outside Count","Not Ok");
}


}




}`

最佳答案

如果您将数组声明为全局数组,那么在将位图添加到位图数组中后,您必须更新适配器并且它应该可以工作。但是,请小心,因为这需要时间,并且应该在异步中完成,特别是当图像又大又重时。

public void done(byte[] data, ParseException e) {
if(e== null && data != null){
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
if(bitmap != null){
Log.i("image download","Success");
bp = bitmap;
Log.i("image Content",bp.toString());
prod_image.add(bp);
//----------------- UPDATE YOUR ARRAY-ADAPTER HERE-----------------
yourView.setAdapter(prod_image);
enter code here

关于java - 使用 ArrayList 通过 ParseFile 下载多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50774026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com