Unable to start download activity from recycler view adapter

0

I am not able to launch an activity from my recycler view adapter. I wish to download a pdf file from firebase. I saw several posts mentioning the passing of context but I have done that. I also checked if the context is being passed using an if statement and if clause is getting executed. I don't know what is the reason behind this error.

This is my adapter code

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

private Context context;
private ArrayList<Upload> noteslist;
private Downloader downloader;
public  static  final String TAG = "NotesAdapter";

public NotesAdapter(Context context, ArrayList<Upload> noteslist) {
    this.context = context;
    this.noteslist = noteslist;
}

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

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    final Upload notes = noteslist.get(position);
    holder.title.setText(notes.getName());
    holder.author.setText(notes.getAuthor());
    holder.noOfDown.setText("No. of Downloads = "+String.valueOf(notes.getDownload()));

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(context instanceof DownloadNotes) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(notes.getUrl()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);      <-------Error
            }
        }
    });
}

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

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView title,author,noOfDown;
    View view ;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        title = itemView.findViewById(R.id.title);
        author = itemView.findViewById(R.id.authorname);
        noOfDown = itemView.findViewById(R.id.download);
        this.view=itemView;
    }
}

This is the error I get

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.collegeconnect, PID: 32314
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=com.google.android.gms.tasks.zzu@ab761b0 flg=0x10000000 }
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2007)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673)
    at android.app.Activity.startActivityForResult(Activity.java:4586)
    at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:675)
    at android.app.Activity.startActivityForResult(Activity.java:4544)
    at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:662)
    at android.app.Activity.startActivity(Activity.java:4905)
    at android.app.Activity.startActivity(Activity.java:4873)
    at com.example.collegeconnect.NotesAdapter$1.onClick(NotesAdapter.java:72)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
android
firebase
asked on Stack Overflow Mar 24, 2020 by Saksham Pruthi

1 Answer

0

You are passing a value that is not a URL:

com.google.android.gms.tasks.zzu@ab761b0

You will have to debug your app to figure out where this incorrect value is coming from, and correct it.

If you are using Cloud Storage for Firebase, you are probably making the very common mistake describing in this other question: How to get URL from Firebase Storage getDownloadURL

answered on Stack Overflow Mar 24, 2020 by Doug Stevenson

User contributions licensed under CC BY-SA 3.0