'How to Download a file from Firebase Realtime Database using Intents

Basically, I'm working on an app where the uploading and retrieval of files are done at Firebase Realtime Database. I have successfully achieved the uploading part by using (intent.setType("* / *") --> to upload all types of files), but the retrieval part is done up to half extent.

To elaborate, I was able to successfully list all the documents into the recycler view, and what I am trying to achieve is, on clicking the document in the RecyclerView should open a browser and download the file.

So I have tried to write a code, where on clicking the document in the RecyclerView asks to select the application, but there is no browser in the list.

Here is the code ---

public class ai_unit_1_recyclerview extends RecyclerView.Adapter <ai_unit_1_recyclerview.ViewHolder> {

    RecyclerView recyclerView;
    Context context;
    public ArrayList<String> items;
    public ArrayList<String> urls;

    public void update(String name,String url){
        items.add(name);
        urls.add(url);
        notifyDataSetChanged();
    }

    public ai_unit_1_recyclerview(RecyclerView recyclerView, Context context, ArrayList<String> items, ArrayList<String> urls) {
        this.recyclerView = recyclerView;
        this.context = context;
        this.items = items;
        this.urls = urls;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.ai_unit_1_items,parent,false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //initialise items of individual items...
        holder.ai_unit_1_files.setText(items.get(position));
    }

    @Override
    public int getItemCount() {//return the number of items
        return items.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        TextView ai_unit_1_files;

        public ViewHolder(View itemView){ //represents individual list items
            super(itemView);
            ai_unit_1_files=itemView.findViewById(R.id.ai_unit_1_files);
            itemView.setOnClickListener(view -> {

                int position=recyclerView.getChildLayoutPosition(view);
                Intent intent=new Intent(ACTION_VIEW);
                intent.setDataAndType(Uri.parse(urls.get(position)),"*/*");
                context.startActivity(intent);
            });
        }
    }
}

. . . . .

I have changed the code by a little, which throws an error "android.content.ActivityNotFoundException"

Here is the changed code ---


        public ViewHolder(View itemView){ //represents individual list items
            super(itemView);
            ai_unit_1_files=itemView.findViewById(R.id.ai_unit_1_files);
            itemView.setOnClickListener(view -> {

                int position=recyclerView.getChildLayoutPosition(view);
                Intent intent=new Intent();
                intent.setType(ACTION_VIEW);
                intent.setData(Uri.parse(urls.get(position)));
                context.startActivity(intent);
            });
        }
    }
}

LOGCAT for the error ---

android.content.ActivityNotFoundException: No Activity found to handle Intent { dat=com.google.android.gms.tasks.zzu@b568eb4 }
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2258)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1914)
        at android.app.Activity.startActivityForResult(Activity.java:5326)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:675)
        at android.app.Activity.startActivityForResult(Activity.java:5284)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:662)
        at android.app.Activity.startActivity(Activity.java:5670)
        at android.app.Activity.startActivity(Activity.java:5623)
        at com.example.ngitstudentportal.ai_unit_1_recyclerview$ViewHolder.lambda$new$0$com-example-ngitstudentportal-ai_unit_1_recyclerview$ViewHolder(ai_unit_1_recyclerview.java:68)
        at com.example.ngitstudentportal.ai_unit_1_recyclerview$ViewHolder$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
        at android.view.View.performClick(View.java:7520)
        at android.view.View.performClickInternal(View.java:7489)
        at android.view.View.access$3600(View.java:826)
        at android.view.View$PerformClick.run(View.java:28555)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:233)
        at android.app.ActivityThread.main(ActivityThread.java:8030)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source