Question

I need a class to get the artists, albums and tracks on the device, which I will then use JNI to call upon.

At the moment, in its barebones, the following causes a crash.

public class AndroidMediaLibray extends Activity {
    public void getArtists() {
        getContentResolver();
    }
}

How do I get this to not crash?

Was it helpful?

Solution

The problem you have, is that you need to call getContentResolver() on a Context. If you call it in an Activity, you automatically call it on the Context of the Activity. But you (probably) never really start AndroidMediaLibrary. Please refer to the documentation of activities. If you want to have the DB call in an extra class, you may have a look at the following code. I have created a class with static methods. I just need to pass the context of my given Activity to that class. In your case that class might look like this:

public class AndroidMediaLibrary {

    public static List<String> getArtists(Context context){

        ArrayList<String> retVal = new ArrayList<String>();

        ContentResolver resolver = context.getContentResolver();        

        // some more stuff here.. 

        return retVal;
    }
}

You may call that function from your MainActivity with:

List<String> myValues = DBUtils.someFunction(MainActivity.this);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top