Question

The situation is that I have a MainFragment (extends a ListFragment, implements LoaderManager.LoaderCallbacks<Cursor>) which is called from MainActivity. A new activity DetailsActivity is called from that fragment to display the content of a certain item in a list view getting the id as an intent argument. How can I getContentResolver() to access its methods using a Uri?

The error right now I get from accessing content resolver is:

`java.lang.IllegalArgumentException: Unknown URL content://com.xxxx.words.contentprovider/words`

uri = Uri.parse(MainContentProvider.CONTENT_URI.toString());
            getContentResolver().insert(uri, contentValues);

CONTENT_URI = Uri.parse("content://" + "com.xxxx.words.contentprovider" + "/" + "words");

android manifest file:

<provider
        android:authorities="com.xxxx.words.contentprovider.MainContentProvider"
        android:name=".contentprovider.MainContentProvider"/>

or should i just return a Bundle from that DetailsActivity and access the content provider in the initial fragment?

Was it helpful?

Solution 3

The error right now I get from accessing content resolver is

That is because your Uri does not match your android:authorities. Your android:authorities is com.xxxx.words.contentprovider.MainContentProvider. Your Uri uses com.xxxx.words.contentprovider. These are not the same, and they need to be the same.

Also, please add android:exported to your <provider>, set to true (if third-party apps should have complete access to the provider) or false (if they should not).

OTHER TIPS

Just simply use this:

ContentResolver resolver = getActivity().getContentResolver();

For Java

ContentResolver resolver = getActivity().getContentResolver();

For Kotlin

private var resolver = requireActivity().contentResolver

This works.

ContentResolver resolver = getActivity().getContentResolver();

In Activity simply pass this,when Fragment pass getActivity().

ContentResolver resolver = getActivity().getContentResolver();

1.Declare variable in your fragment class :

private Context context;

2.Now In onCreateView(inflator,container,savedInstanceState)method of your fragment you will do following :

context = container.getContext();

3.Now you can call your getContentResolver() method or any method which you can call in your main activity can also be called here by using this context variable:

ContentResolver contentResolver = context.getContentResolver();

Your code will look like this:

    public class your_fragment extends Fragment {

         private Context;

         @override
         public View onCreateView(LayoutInflator inflator,ViewGroup Container,Bundle savedInstancestate) 
         {
            context = container.getContext();
            View root = inflator.inflate(R.id.your_Layout_of_frag);
            return root;
            
         }
    }

This example works for Kotlin

 val resolver = context?.contentResolver?.openFileDescriptor(....)

Cheers!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top