从Amazon S3下载更多图像并在RecycleView中显示它们

时间:2015-09-04 16:10:55

标签: android amazon-web-services amazon-s3 android-asynctask android-recyclerview

我想从Amazon S3下载图像并在RecycleView中显示它们 我想要一种更有效的下载图像的方法(只有在需要在屏幕上显示时下载图像),如Carousell App。 Carousell App显示描述项目,然后在显示项目描述后显示图像 这可能会显示在RecycleView中从Amazon-s3下载图像的项目 OwnerActivityDetails

public class OwnerActivityDetails extends AppCompatActivity implements OwnerItemsAdapter.ItemClick {

Toolbar toolbar;
List<Item>itemList=new ArrayList<>();
ProgressBar bar;
ParallaxRecyclerAdapter<Item> adapter;
RecyclerView myRecycler;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.owner_activity);
    myRecycler = (RecyclerView) findViewById(R.id.ownerItems);
    toolbar=(Toolbar)findViewById(R.id.app_bar_user);
    Constants.MANAGER=new AmazonClientManager(this);
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    bar=(ProgressBar)findViewById(R.id.progressBar5);
    new ShowOwneritems().execute();
}

private void createAdapter() {
    adapter=new OwnerItemsAdapter(itemList,OwnerActivityDetails.this);
    myRecycler.setLayoutManager(new LinearLayoutManager(OwnerActivityDetails.this));
    View header = getLayoutInflater().inflate(R.layout.owner_header, myRecycler, false);
    TextView aboutuser=(TextView)header.findViewById(R.id.aboutUser);
    ImageView imageowner=(ImageView)header.findViewById(R.id.userImage);
    aboutuser.setText(Constants.userinfo.getAbout());
    imageowner.setImageBitmap(Constants.userinfo.getUserBitmap());
    adapter.setParallaxHeader(header,myRecycler);
    adapter.setData(itemList);
    myRecycler.setAdapter(adapter);
}

@Override
public void itemClickView(View view, int position) {

    Toast.makeText(OwnerActivityDetails.this,"Position"+position,Toast.LENGTH_LONG).show();
    Constants.itemclick=itemList.get(position);
    Intent i=new Intent("com.sprintone.userInterface.ItemDescriptionActivity");
    startActivity(i);
}

public class ShowOwneritems extends AsyncTask<Void,Void,Void>{

    @Override
    protected void onPreExecute() {
        bar.setVisibility(View.VISIBLE);
    }

    @Override
    protected Void doInBackground(Void... params) {
    /* retrieve items for owner and each item has S3link object to download image from
        Amazon-s3
    */
        itemList= DynamoDBManager.retrieveItemsOwner("<email>");   

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        if(itemList==null) {
            itemList=new ArrayList<>();
            Log.e("OWNER_DEATAILS_ACTIVITY","NULLABLE");
            createAdapter();
            bar.setVisibility(View.GONE);
        }
        else{
            new DownloadImage().execute();
        }

    }
}

    public class DownloadImage extends AsyncTask<List<Item>,Void,Void>{
    @Override
    protected Void doInBackground(List<Item>... params) {
        InputStream myObjectBytes=null;
        S3Object getResponse=null;
        GetObjectRequest getRequest;
        for (int i=0;i<itemList.size();i++) {
            String bucketName= itemList.get(i).getItemPhoto1().getBucketName();
            String pictureId= itemList.get(i).getItemPhoto1().getKey();
            getRequest = new GetObjectRequest(bucketName,pictureId);
            getResponse = s3Client.getObject(getRequest);
            myObjectBytes = getResponse.getObjectContent();
            itemList.get(i).setBitmap(BitmapFactory.decodeStream(myObjectBytes));
            Log.e(TAG,"download"+i);
        }
        try {
            myObjectBytes.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        createAdapter();
        bar.setVisibility(View.GONE);

    }
}   

}

此OwnerItemsAdapter类

public class OwnerItemsAdapter extends ParallaxRecyclerAdapter<Item> {

private LayoutInflater inflater;    // used to replcae fragment

public ItemClick getClick() {
    return click;
}

public void setClick(ItemClick click) {
    this.click = click;
}
private ItemClick click;
public OwnerItemsAdapter(List<Item> data,Context context) {
    super(data);
    inflater=LayoutInflater.from(context);
}

@Override
public void onBindViewHolderImpl(RecyclerView.ViewHolder viewHolder, ParallaxRecyclerAdapter<Item> parallaxRecyclerAdapter, int i) {
    ((ViewHolder) viewHolder).itemTitle.setText(parallaxRecyclerAdapter.getData().get(i).getItemTitle());
    ((ViewHolder) viewHolder).itemprice.setText(parallaxRecyclerAdapter.getData().get(i).getItemPrice()+"$");
    ((ViewHolder) viewHolder).imageView.setImageBitmap(parallaxRecyclerAdapter.getData().get(i).getBitmap());
}

@Override
public RecyclerView.ViewHolder onCreateViewHolderImpl(ViewGroup viewGroup, ParallaxRecyclerAdapter<Item> parallaxRecyclerAdapter, int i) {
    View view=inflater.inflate(R.layout.view_item_owner, viewGroup, false);
    ViewHolder holder=new ViewHolder(view);
    return holder;
}

@Override
public int getItemCountImpl(ParallaxRecyclerAdapter<Item> parallaxRecyclerAdapter) {
    return parallaxRecyclerAdapter.getData().size();
}

 class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView itemTitle;
    public TextView itemprice;
    public ImageView imageView;
    public ViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        itemTitle = (TextView) itemView.findViewById(R.id.ownerItemTitle);
        itemprice=(TextView)itemView.findViewById(R.id.ownerItemprice);
        imageView=(ImageView)itemView.findViewById(R.id.imageItemOwner);
    }

    @Override
    public void onClick(View v) {
        if(click!=null){
            click.itemClickView(itemView,getPosition());
        }
    }
}

public interface ItemClick {
    public void itemClickView(View view,int position);
}

}

0 个答案:

没有答案