图像将RecyclerView保存到图库中

时间:2016-12-12 10:47:11

标签: android firebase nullpointerexception android-recyclerview firebase-realtime-database

我已经在RecyclerView中检索了图像,现在我想要在每个图像项目被点击时,图像将自己保存到图库中。

这是我如何将我的图像设置为适配器中的ImageView。

viewHolder.setImage(c, model.getEventImage());

然后,这就是我如何处理适配器中的click事件。

 viewHolder.imagePost.setOnClickListener(new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {

                             viewHolder.SaveImage(viewHolder.imagePost);
                         }
                     });

这是我的Viewholder课,我宣布了我的ImageView

public static class BlogViewHolder extends RecyclerView.ViewHolder{


        View mView;

        private ImageView imagePost;
        private  ImageView save_image_blog;
        private FullscreenVideoLayout videoLayout;
        private Context cx;
         ImageView videoView;
        public BlogViewHolder(View itemView) {
            super(itemView);
            mView=itemView;
            imagePost =(ImageView)mView.findViewById(R.id.post_image);
             save_image_blog =(ImageView)mView.findViewById(R.id.save_image_blog);
            videoLayout = (FullscreenVideoLayout) mView.findViewById(R.id.post_video);
            videoView = (ImageView) mView.findViewById(R.id.videoview);



        }


        public void setImage(final Context c,final String imageUrl) {


            int size = (int) Math.ceil(Math.sqrt(MAX_WIDTH * MAX_HEIGHT));
            //

            Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).placeholder(R.mipmap.add_btn)
                    .transform(new BitmapTransform(MAX_WIDTH, MAX_HEIGHT))
                    .skipMemoryCache()
                    .networkPolicy(NetworkPolicy.OFFLINE).into(imagePost, new Callback() {
                @Override
                public void onSuccess() {

                }

                @Override
                public void onError() {

                    //Reloading an image again ...
                    Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).placeholder(R.mipmap.add_btn)
                            .into(imagePost);
                }
            });


        }

                   public  void SaveImage(ImageView img){

            img.buildDrawingCache();

            Bitmap bmp = img.getDrawingCache();

            File storageLoc = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); //context.getExternalFilesDir(null);

            File file = new File(storageLoc, "Pixsar" + ".jpg");

            try{
                FileOutputStream fos = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                fos.close();

                scanFile(cn, Uri.fromFile(file));

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            Toast.makeText(cn,"Image Saved to gallery ",Toast.LENGTH_LONG).show();
        }


    }

问题是当我尝试点击Image我得到一个空指针时,下面是logcat。

java.lang.NullPointerException
                                                                                  at company.override.huzykamz.pixsar.BlogScrolling$BlogViewHolder.SaveImage(BlogScrolling.java:343)
                                                                                  at company.override.huzykamz.pixsar.BlogScrolling$2$1.onClick(BlogScrolling.java:210)
                                                                                  at android.view.View.performClick(View.java:4438)
                                                                                  at android.view.View$PerformClick.run(View.java:18422)
                                                                                  at android.os.Handler.handleCallback(Handler.java:733)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:136)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                                  at dalvik.system.NativeStart.main(Native Method)

以下是具体的行。

 at company.override.huzykamz.pixsar.BlogScrolling$BlogViewHolder.SaveImage(BlogScrolling.java:343)
                                                                                  at company.override.huzykamz.pixsar.BlogScrolling$2$1.onClick(BlogScrolling.java:210)

我的完整适配器位于

之下
 try {
            FirebaseRecyclerAdapter<Posts, BlogScrolling.BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posts, BlogScrolling.BlogViewHolder>(Posts.class,
                    R.layout.item_activity,
                    BlogScrolling.BlogViewHolder.class,
                    mDatabase) {
                @Override
                protected void populateViewHolder(final BlogScrolling.BlogViewHolder viewHolder, final Posts model, final int position) {

                    final String key_post = getRef(position).getKey();
                    final String videoUrl = model.getEventVideo();

                    try {
                        viewHolder.setTitle(model.getEventTitle());
                        viewHolder.setDesc(model.getEventDescription());
                        viewHolder.setImage(c, model.getEventImage());
                        viewHolder.setGif(c,model.getEventVideo());





                     viewHolder.imagePost.setOnClickListener(new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {
                             BlogViewHolder holder = (BlogViewHolder)(v.getTag());
                             holder.SaveImage(holder.imagePost);
                         }
                     });

                    viewHolder.videoView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //          Toast.makeText(getApplicationContext()," position "+ position,Toast.LENGTH_LONG);
                            Intent i = new Intent(BlogScrolling.this,VideoActivity.class);
                            i.putExtra("VideoUrl", videoUrl);
                            startActivity(i);
                        }
                    });}


                    catch (NullPointerException e){

                }
                //    final String key_post_image = getRef(position).getKey();




                }
            };

            mRecyclerview.setAdapter(firebaseRecyclerAdapter);

2 个答案:

答案 0 :(得分:0)

你在onClick监听器中没有viewHolder对象。 请检查此答案:getting-a-reference-to-viewholder-on-recyclerview-click

组 BlogViewHolder构造函数中的imagePost.setTag(this)。 并在 public void onClick(View v) 做:

BlogViewHolder viewHolder= (BlogViewHolder)(v.getTag());
viewHolder.SaveImage(viewHolder.imagePost);

答案 1 :(得分:0)

populateViewHolder()

内的setTag

imagePost.setTag(viewHolder)

并在public void onClick(View v)中执行:

BlogViewHolder viewHolder= (BlogViewHolder)(v.getTag());
viewHolder.SaveImage(viewHolder.imagePost);