0

I went through almost all what is available on internet about downloading a file from internet and one of the tutorials worked
In that tutorial they've used download manager but it was for a button ..i'm using a recyclerview with items having a single dwonload button and each download button of the items list has a different url , please show me how to do that!
what i've tried:( i know it's not the right syntax for personViewHolder.personbutton.setOnClickListener but just to show you what i kinda want)

Photo from the emulator of my app!

pic

RVadapter.java

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {


    public static class PersonViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView personName;
        TextView personAge;
        ImageView personPhoto;
        Button personbutton;

        PersonViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            personName = (TextView)itemView.findViewById(R.id.person_name);
            personAge = (TextView)itemView.findViewById(R.id.person_age);
            personPhoto = (ImageView)itemView.findViewById(R.id.person_photo);
            personbutton=(Button)itemView.findViewById(R.id.imgbuttondownload);

        }
    }

    List<Person> persons;

    RVAdapter(List<Person> persons){
        this.persons = persons;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(PersonViewHolder personViewHolder, final int i) {
        personViewHolder.personName.setText(persons.get(i).name);
        personViewHolder.personAge.setText(persons.get(i).age);
        personViewHolder.personPhoto.setImageResource(persons.get(i).photoId);
        personViewHolder.personPhoto.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                Context context=v.getContext();
                Intent intent=new Intent();
                switch (i){
                    case 0:
                        intent =  new Intent(context, MainActivity.class);
                        context.startActivity(intent);
                        break;
                    case 1:
                        intent =  new Intent(context, MainActivity.class);
                        context.startActivity(intent);
                        break;
                    case 2:
                        intent =  new Intent(context, MainActivity.class);
                        context.startActivity(intent);
                        break;
                    case 3:
                        intent =  new Intent(context, MainActivity.class);
                        context.startActivity(intent);
                        break;
                }
            }
        });

        personViewHolder.personbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (i){
                    case 0:
                String url = "http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg";
                        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                request.setDescription("my title");
                request.setTitle("my description");

                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(
                        DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file"+i+".mp4");
                        manager.enqueue(request);
                break;
                    case 1:
                        String url2 = "http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg";
                        DownloadManager manager2 = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                        DownloadManager.Request request2 = new DownloadManager.Request(Uri.parse(url2));
                        request2.setDescription("my title");
                        request2.setTitle("my description");

                        request2.allowScanningByMediaScanner();
                        request2.setNotificationVisibility(
                                DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

                        request2.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file"+i+".mp4");
                        manager2.enqueue(request2);
                        break;


            }
        }
    });
    }

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

Mainactivity.java

public class Main4Activity extends Activity {

    private List<Person> persons;
    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main4);

        rv=(RecyclerView)findViewById(R.id.rv);

        LinearLayoutManager llm = new LinearLayoutManager(this);
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true);


        initializeData();
        initializeAdapter();

    }



    private void initializeData(){
        persons = new ArrayList<>();
        persons.add(new Person("How to perform a staphylectomy | Surgical correction of RLN", "10:07", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));
        persons.add(new Person("How to perfom a clinical examination", "07:03", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));
        persons.add(new Person("How to perform a radiographic examination", "03:50", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));
        persons.add(new Person("How to perform a endoscopic examination", "03:50", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));
        persons.add(new Person("How to perform a staphylectomy | Surgical correction of RLN", "03:50", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));
        persons.add(new Person("How to perform a radiographic examination", "03:50", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));
        persons.add(new Person("How to perform a radiographic examination", "03:50", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));
        persons.add(new Person("How to perform a radiographic examination", "03:50", R.drawable.chirurgie,"http://www.androidtutorialpoint.com/wp-content/uploads/2016/09/Beauty.jpg"));

    }

    private void initializeAdapter(){
        RVAdapter adapter = new RVAdapter(persons);
        rv.setAdapter(adapter);
    }

}
karthik
  • 528
  • 4
  • 19
Zouhair
  • 145
  • 1
  • 2
  • 13
  • 1
    Possible duplicate of [Simple Android RecyclerView example](https://stackoverflow.com/questions/40584424/simple-android-recyclerview-example) – Shubham AgaRwal Aug 12 '18 at 03:39
  • With all respect ..my subject has nothing related to what you claim is duplicate of! i'm trying to add a download manager to my recycler view..if you want to help that would be great by providing code! not by suggesting that is dupplicate – Zouhair Aug 12 '18 at 11:01
  • Congratulation zouhair. I see you have got the answer. But if you see this question again after a few months from now while developing Android apps. You will surely get the idea of why it is duplicate. I am sure nobody has asked how to use download manager in RecyclerView. But the problem is same. If you get the idea why RecyclerView is called recycler view and how it works then I am sure you can solve it yourself. BTW welcome to the community. – Shubham AgaRwal Aug 12 '18 at 13:06
  • @Killer Thanks dude, just one last thing, do you know how to generate a link that ends with .mp4 from youtube video? because the urls that i need to pass should end with .mp4 or something ..thanks again – Zouhair Aug 12 '18 at 13:19
  • No, you can't do that with youtube videos. – Shubham AgaRwal Aug 12 '18 at 13:20
  • @Killer Thanks for the answer, and how can i make a video that i have in my computer to be donwloaded from internet and get a direct link of it and ends with mp4 ..i mean do you know any website that i can upload to and that would gives me direct link tha ends with .mp4? – Zouhair Aug 12 '18 at 13:22
  • You need a hosting server/Cloud. Check firebase cloud storage . But i am sure that will cost you heavily & copyright issues. Try some other alternate. – Shubham AgaRwal Aug 12 '18 at 13:24
  • Okey thanks for the answer – Zouhair Aug 12 '18 at 13:26

1 Answers1

1

you can simply hold your URL in Person object and then whenever a click happens just retrieve it and send it to download manager. it will be something like this:

personViewHolder.personbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                    Person person = persons.get(i);
                    String url = person.url;
                    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                    request.setDescription(person.name);
                    request.setTitle(person.description);

                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(
                    DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file"+person.name+".mp4");
                    manager.enqueue(request);
        }});
seyed Jafari
  • 1,235
  • 10
  • 20
  • seyed thank(chokran xD) for the answer..by your answer do you mean i should just add this code one time and the code will be applied to all of them through the url that will be retrieved from the person.url? ..and my second question is getSystemService is hilighted red and i don't know how to solve the problem – Zouhair Aug 12 '18 at 11:08