使用毕加索异步加载图像

时间:2016-09-20 06:59:10

标签: android picasso

我想在回收站视图滚动时加载图像。为此,我使用毕加索。

我正在创建一个文件来将图像存储在毕加索中。但它没有被创造出来。当我尝试调试时,发现它在.into()之后没有运行代码,它直接转到complesPreferences代码。

因此,当complexPreferences保持为null时,它会给出空指针异常。

contactAdapter:

    public class ContactAdapter extends RecyclerView.Adapter<FeedListRowHolder> {


    private List<FeedItem> feedItemList;

    private Context mContext;

    public ContactAdapter(Context context, List<FeedItem> feedItemList) {
        this.feedItemList = feedItemList;
        this.mContext = context;
    }

    @Override
    public FeedListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout,null);
        FeedListRowHolder mh = new FeedListRowHolder(v);

        return mh;
    }

    @Override
    public void onBindViewHolder(FeedListRowHolder feedListRowHolder, int i) {
        final FeedItem feedItem = feedItemList.get(i);
        Log.e("Imagename",""+"http://xesoftwares.co.in/contactsapi/profile_images/85368a5bbd6cffba8a3aa202a80563a2.jpg");//+feedItem.getThumbnail());


       Picasso.with(mContext).load(feedItem.getprofileImage())
                .error(R.drawable.ic_account_circle_black_24dp)
                .placeholder(R.drawable.ic_account_circle_black_24dp)
                .resize(100,100)
                .into(new Target() {

                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        try {
                            String root = Environment.getExternalStorageDirectory().getPath();
                            File myDir = new File(root +"/Contact");

                            if (!myDir.exists()) {
                                myDir.mkdirs();
                            }

                           // String name = new Date().toString();
                            String name = new Date().toString()+".jpg";
                          File  myDir1 = new File(myDir, name);

                            FileOutputStream out = new FileOutputStream(myDir1);
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

                            ImageFilePath imageFilePath1=new ImageFilePath(myDir1);
                            ComplexPreferences complexPreferences112 = ComplexPreferences.getComplexPreferences(mContext, "mypref112", Context.MODE_PRIVATE);
                            complexPreferences112.putObject("imageFilePath1", imageFilePath1);
                            Log.e("user2", "" + imageFilePath1);
                            complexPreferences112.commit();
                            out.flush();
                            out.close();
                        } catch(Exception e){
                            // some action
                        }
                    }

                    public void onBitmapFailed(Drawable errorDrawable) {
                    }

                    public void onPrepareLoad(Drawable placeHolderDrawable) {
                    }
                });

       ComplexPreferences complexPreferences112 = ComplexPreferences.getComplexPreferences(mContext, "mypref112", mContext.MODE_PRIVATE);
        ImageFilePath imageFilePath1= complexPreferences112.getObject("imageFilePath1", ImageFilePath.class);
        File myDir1=imageFilePath1.getprofile();

       Picasso.with(mContext).load(myDir1).into(feedListRowHolder.thumbnail);

        feedListRowHolder.title.setText(Html.fromHtml(feedItem.getTitle()));
        //feedListRowHolder.genre.setText(Html.fromHtml(feedItem.getGenre()));

    }

    @Override
    public int getItemCount() {
        return (null != feedItemList ? feedItemList.size() : 0);
    }
}

任何人都可以帮忙吗?为什么它不起作用..谢谢..

编辑:我试过这样:

仍然没有调用我认为因为仍然在complexPreferences上获得空指针异常。

public class ContactAdapter extends RecyclerView.Adapter<FeedListRowHolder> {


    private List<FeedItem> feedItemList;

    private Context mContext;

    public ContactAdapter(Context context, List<FeedItem> feedItemList) {
        this.feedItemList = feedItemList;
        this.mContext = context;
    }

    @Override
    public FeedListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout,null);
        FeedListRowHolder mh = new FeedListRowHolder(v);

        return mh;
    }

    @Override
    public void onBindViewHolder(FeedListRowHolder feedListRowHolder, int i) {
        final FeedItem feedItem = feedItemList.get(i);
        Log.e("Imagename",""+"http://xesoftwares.co.in/contactsapi/profile_images/85368a5bbd6cffba8a3aa202a80563a2.jpg");//+feedItem.getThumbnail());


        Target target = new Target () {
            // your code here ...

            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                try {
                    String root = Environment.getExternalStorageDirectory().getPath();
                    File myDir = new File(root +"/Contact");

                    if (!myDir.exists()) {
                        myDir.mkdirs();
                    }

                    // String name = new Date().toString();
                    String name = new Date().toString()+".jpg";
                    File  myDir1 = new File(myDir, name);

                    FileOutputStream out = new FileOutputStream(myDir1);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

                    ImageFilePath imageFilePath1=new ImageFilePath(myDir1);
                    ComplexPreferences complexPreferences112 = ComplexPreferences.getComplexPreferences(mContext, "mypref112", Context.MODE_PRIVATE);

                    complexPreferences112.putObject("imageFilePath1", imageFilePath1);
                    Log.e("user2", "" + imageFilePath1);
                    complexPreferences112.commit();
                    out.flush();
                    out.close();
                } catch(Exception e){
                    // some action
                }
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {
            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {
            }

        };
        feedListRowHolder.thumbnail.setTag(target);

        Picasso.with(mContext).load(feedItem.getprofileImage())
                .error(R.drawable.ic_account_circle_black_24dp)
                .placeholder(R.drawable.ic_account_circle_black_24dp)
                .resize(100,100)
                .into(target);


       ComplexPreferences complexPreferences112 = ComplexPreferences.getComplexPreferences(mContext, "mypref112", mContext.MODE_PRIVATE);
        ImageFilePath imageFilePath1= complexPreferences112.getObject("imageFilePath1", ImageFilePath.class);

            File myDir1 = imageFilePath1.getprofile();

            Picasso.with(mContext).load(myDir1).into(feedListRowHolder.thumbnail);

        feedListRowHolder.title.setText(Html.fromHtml(feedItem.getTitle()));
        //feedListRowHolder.genre.setText(Html.fromHtml(feedItem.getGenre()));

    }

    @Override
    public int getItemCount() {
        return (null != feedItemList ? feedItemList.size() : 0);
    }
}

0 个答案:

没有答案