1

I am developing an application where particular files will be listed in a list view. Now when a user clicks on an item in the list the selected file should open in its default handler. For eg: if the user clicks on a song in the list, the song must start playing in the default player configured on the user's device and so on for images, documents etc.

I have written the following code to load all the audio files (for the time being) in the list. Now i am stuck on the onClickListener. I don't know how to proceed so that when i click on a file in the list it starts playing in my default music player.

package com.example.listviewexample;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);

    String selection=MediaStore.Audio.Media.IS_MUSIC+ " != 0";
    String projection[]={MediaStore.Audio.Media.TITLE};
    Cursor cursor=this.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);

    final ListView listView=(ListView) findViewById(R.id.listview);
    final ArrayList<String> arrayList=new ArrayList<String>();

    while(cursor.moveToNext()){
        arrayList.add(cursor.getString(0));
    }

    final StableArrayAdapter adapter=new StableArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

        @SuppressLint("NewApi")
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position,
                long id) {
            final String item=(String) parent.getItemAtPosition(position);
            Toast.makeText(getApplicationContext(), "Currently Selected Song:"+item, Toast.LENGTH_SHORT).show();
        }

    });
}

private class StableArrayAdapter extends ArrayAdapter<String>{

    HashMap<String, Integer> mIdMap=new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,List<String> objects) {
        super(context, textViewResourceId,objects);

        for(int i=0;i<objects.size();i++)
        {
            mIdMap.put(objects.get(i), i);
        }
    }

    @Override
    public long getItemId(int position) {
        String item=getItem(position);
        return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

}

}

Edit: Based on the suggestions i tried the following code:

Intent intent=Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_MUSIC);
startActivity(intent);

Now the music player is launching. But how do i tell the music player to play a particular file only?

Nikhil Joshi
  • 87
  • 1
  • 6

2 Answers2

1

This is how you open a default android browser(taken from here)

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);

Falowing the same logic I found this Intent CATEGORY_APP_MUSIC. You need to use it with ACTION_MAIN. Try it I hope it will work for you.

Community
  • 1
  • 1
Ilya Gazman
  • 27,805
  • 19
  • 119
  • 190
0

Try this:

    Intent notificationIntent = new Intent(Intent.ACTION_VIEW, fileUri);                        
    startActivity(notificationIntent);
Bolton
  • 2,146
  • 3
  • 24
  • 29