RbkGh / WSDownloader

Download Whatsapp Status Media of Your Friends & Family Without Asking them Directly.
26 stars 26 forks source link

Images And Videos keeps on repeating after showing 4 files properly #9

Open MelvinMathew9 opened 6 years ago

MelvinMathew9 commented 6 years ago

JUST SET THE VISIBILITY BACK ON ELSE PART IN onBindViewHolder SNIPPET @Override public void onBindViewHolder(@NonNull final RecyclerViewMediaAdapter.FileHolder holder, int position) { File currentFile = filesList.get(position);

    String DIRECTORY_TO_SAVE_MEDIA_NOW = "/storage/emulated/0/Whats Anyone/";
    holder.buttonImageDownload.setOnClickListener(this.downloadMediaItem(currentFile, DIRECTORY_TO_SAVE_MEDIA_NOW,"Saved To Gallery"));
    holder.buttonVideoDownload.setOnClickListener(this.downloadMediaItem(currentFile, DIRECTORY_TO_SAVE_MEDIA_NOW,"Saved To Gallery"));
    holder.bookmark.setOnClickListener(this.downloadMediaItem(currentFile,"/Whats Storage/","Bookmarked"));
    if (currentFile.getAbsolutePath().endsWith(".mp4")) {
        holder.cardViewImageMedia.setVisibility(View.GONE);
        holder.cardViewVideoMedia.setVisibility(View.VISIBLE);
        Uri video = Uri.parse(currentFile.getAbsolutePath());
        holder.videoViewVideoMedia.setVideoURI(video);
        holder.videoViewVideoMedia.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(true);
                holder.videoViewVideoMedia.start();
            }
        });
    } else {
        holder.cardViewImageMedia.setVisibility(View.VISIBLE);
        holder.cardViewVideoMedia.setVisibility(View.GONE);
        Glide.with(activity).load(currentFile.getAbsolutePath()).into(holder.imageViewImageMedia);
    }
RbkGh commented 6 years ago

@Melvin9, I'd really love it if you could find some small time to send a PR. Would really appreciate that. Looking forward to that.

Prince655 commented 6 years ago

it is working pretty good but how to stop music???, music is still.

RbkGh commented 6 years ago

Hello @Prince655, can you kindly send a PR with @Melvin9 's code he has posted? From there, we can look at how to stop music like you are saying.

MukarramAhsen commented 6 years ago

same error images repeated

Prince655 commented 6 years ago

Check apk on play store that I have built. Link:http://play.google.com/store/apps/details?id=com.glowrm.status_downloader

ahmadbajwa12315 commented 3 years ago

Try This Adapter

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Objects;

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.MyViewHolder> {
    final Context context;
    final ArrayList<File> modelFeedArrayList;
    private static final String DIRECTORY_TO_SAVE_MEDIA_NOW = "/WhatsappStatus/";

    public ListAdapter(Context context, final ArrayList<File> modelFeedArrayList) {
        this.context = context;
        this.modelFeedArrayList = modelFeedArrayList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_media_row_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {

        File currentFile = modelFeedArrayList.get(position);

        if (currentFile.getAbsolutePath().endsWith(".mp4")) {
            holder.cardViewImageMedia.setVisibility(View.GONE);
            holder.cardViewVideoMedia.setVisibility(View.VISIBLE);
            Uri video = Uri.parse(currentFile.getAbsolutePath());
            holder.videoViewVideoMedia.setVideoURI(video);
            holder.videoViewVideoMedia.setOnPreparedListener(mp -> {
                mp.setLooping(true);
                holder.videoViewVideoMedia.start();
            });
        } else {
            holder.cardViewImageMedia.setVisibility(View.VISIBLE);
            holder.cardViewVideoMedia.setVisibility(View.GONE);
            Bitmap myBitmap = BitmapFactory.decodeFile(currentFile.getAbsolutePath());
            holder.imageViewImageMedia.setImageBitmap(myBitmap);
        }
    }

    public View.OnClickListener downloadMediaItem(final File sourceFile) {
        return v -> ((Runnable) () -> {
            try {
                copyFile(sourceFile, new File(Environment.getExternalStorageDirectory().toString() + DIRECTORY_TO_SAVE_MEDIA_NOW + sourceFile.getName()));
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("RecyclerV", "onClick: Error:" + e.getMessage());

            }
        }).run();
    }

    public static void copyFile(File sourceFile, File destFile) throws IOException {
        if (!Objects.requireNonNull(destFile.getParentFile()).exists())
            destFile.getParentFile().mkdirs();

        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        try (FileChannel source = new FileInputStream(sourceFile).getChannel(); FileChannel destination = new FileOutputStream(destFile).getChannel()) {
            destination.transferFrom(source, 0, source.size());
        }
    }

    @Override
    public int getItemCount() {
        return modelFeedArrayList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView imageViewImageMedia;
        VideoView videoViewVideoMedia;
        CardView cardViewVideoMedia;
        CardView cardViewImageMedia;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageViewImageMedia = itemView.findViewById(R.id.imageViewImageMedia);
            videoViewVideoMedia = itemView.findViewById(R.id.videoViewVideoMedia);
            cardViewVideoMedia = itemView.findViewById(R.id.cardViewVideoMedia);
            cardViewImageMedia = itemView.findViewById(R.id.cardViewImageMedia);

        }
    }
}