Marcus Smith
Marcus Smith

Reputation: 109

incompatible types: cannot be converted to Context

I am and getting an error when trying to modify code to handle documents in Android Nougat.

incompatible types: cannot be converted to Context

This is my code

    documentViewHolder.preview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    File document = new File(Environment.getExternalStorageDirectory() + "/heyJudeDocuments/" + getItem(position).attachment_id);  // -> filename = maven.pdf 
                    Uri path = FileProvider.getUriForFile(MessageAdapter.this,BuildConfig.APPLICATION_ID + ".provider",document);
                    Intent docIntent = new Intent(Intent.ACTION_VIEW);
                    docIntent.setDataAndType(path, getItem(position).mime);
                    docIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    docIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

It's the MessageAdapter.this part that seems to be wrong. Can someone point out where I am going wrong?

Upvotes: 0

Views: 12934

Answers (2)

Sasi Kumar
Sasi Kumar

Reputation: 13278

public class MessageAdapter extends BaseAdapter {
private Context context;

public MessageAdapter(Context context) {
    this.context = context;
}

use that context(instead of MessageAdapter.this) in Uri

 Uri path = FileProvider.getUriForFile(context ,BuildConfig.APPLICATION_ID + ".provider",document);

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1006554

MessageAdapter is not a subclass of Context. Usually, for what you are doing (starting an activity, presumably), you have access to the Activity that you are in, and that is the Context to use here.

Upvotes: 0

Related Questions