Question

i am new to mediastore.i am making an android app,and i wanted to know how i can fetch file path from mediastore?or an artist image? i was using mediametadataretriever earlier instead of mediastore,and i was making up my own database,it worked fine,but it used to lag for 3 secs,and no one wants that. below is the code which is irrelevant to the question i have asked,but i am posting it just because people are in a habit here of down voting if no code has been posted... package sourcecode.jazzplayer;

import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import sourcecode.jazzplayer.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;


public class FragmentSongs extends  Fragment implements Serializable{

    ListView SngList;
     ArrayList<SongDetails> Songinfo=null;
    AdapterView.AdapterContextMenuInfo info;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
    {

        ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_song, null);

        SngList = (ListView) view.findViewById(R.id.SongList);
        registerForContextMenu(SngList);
        //File f=new File(Environment.getExternalStorageDirectory()+"/Music");
        //File f=new File(Environment.getExternalStorageDirectory()+"/musicz");
        File f=new File("/system/");
        //File f=new File("/storage/extSdCard/My Music/");
        int j=0;int i=0;

        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

          Songinfo = getSongsFromDirectory(f);



        //sorting done below
        if (Songinfo.size()>0)
        {

            for( j=0; j<Songinfo.size();j++)
            {
                for ( i=j+1 ; i<Songinfo.size(); i++)
                { 
                    SongDetails a=Songinfo.get(i);
                    SongDetails b=Songinfo.get(j);
                    if(a.getSong().toLowerCase().compareTo(b.getSong().toLowerCase())<0)
                    {   

                        Songinfo.set(i,b );
                        Songinfo.set(j,a);
                    }
                }

            }







                //starting a new activity for playing the music
            SngList.setOnItemClickListener(new OnItemClickListener() {
                 public void onItemClick(AdapterView a, View v, int position, long id) {


                       Intent intent = new Intent(getActivity(), NowPlaying.class);

                       intent.putParcelableArrayListExtra("Data1",Songinfo);

                       intent.putExtra("Data2",position);
                       startActivity(intent);

                             }
                     });



       SngList.setAdapter(new CustomAdapter(Songinfo));
        return view;
        }
        else return null;

    }
    OnFragmentCreatedListener listener;
    @Override
    public void onStart() {
        super.onStart();
        try {
            listener = (OnFragmentCreatedListener) getActivity();
            listener.onFragmentCreated(Songinfo);
        } catch (ClassCastException e) {
            throw new ClassCastException(getActivity().toString() + " must implement onFragmentCreated");
        }
    }


    public ArrayList<SongDetails> getSongsFromDirectory(File f) 
        {  //MediaMetadataRetriever mmr = new MediaMetadataRetriever();

            ArrayList<SongDetails> songs = new ArrayList<SongDetails>();
          //  Bitmap bitmap2; 
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ab);

            float ht_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
            float wt_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());

            Bitmap bitmap3 = Bitmap.createScaledBitmap(bmp, (int) ht_px, (int) wt_px, true);
            //byte[] rawArt = null;
            Bitmap art;
            //BitmapFactory.Options bfo=new BitmapFactory.Options();
            if (!f.exists() || !f.isDirectory()) 
            {    
                return songs;
            }
            File[] files = f.listFiles(new Mp3Filter());
            for(int i=0; i<files.length; i++) 
            { 
                if (files[i].isFile())
                { 
                //      mmr.setDataSource(files[i].getPath());
                 //rawArt = mmr.getEmbeddedPicture();
                    SongDetails detail=new SongDetails(); 
                //if ( rawArt != null) 
                    {   
                    //bitmap2=BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);
                    //bitmap2 = Bitmap.createScaledBitmap(bitmap2, (int) ht_px, (int) wt_px, true);

                    //detail.setIcon(bitmap2);
                }//else 
            {   
                    detail.setIcon(bitmap3);
                }   

                    detail.setSong(files[i].getName()); 
          // detail.setArtist(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)); 
         //  if (mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)==null||(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)==""))
           {  detail.setArtist("Unknow Artist");   }
         //  detail.setAlbum(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)); 
         //  if (mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)==null||mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)=="")
           {  detail.setAlbum("Unknow Album");   }
           detail.setPath2( files[i].getPath()) ;
           songs.add(detail); 
                }
                else if (files[i].isDirectory())
                { 
                songs.addAll(getSongsFromDirectory(files[i])); 
                } 

            }       return songs;
    }

    @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
        {
                        super.onCreateContextMenu(menu, v, menuInfo);      
                       info = (AdapterContextMenuInfo) menuInfo;
                       menu.add(Menu.NONE, v.getId(), 0, "Play");
                       menu.add(Menu.NONE, v.getId(), 0, "Delete");
                       menu.add(Menu.NONE, v.getId(), 0, "Queue Item");                  
               }

        @Override
        public boolean onContextItemSelected(MenuItem item) {
                if (item.getTitle() == "Play") {

                       }
                 else if (item.getTitle() == "Delete") {

                       }

                 else if (item.getTitle() == "Queue Item") {

                       }
                 else     {
                       return false;
                       }
               return true;
               }
        public interface OnFragmentCreatedListener {
            public void onFragmentCreated(ArrayList<SongDetails> msg);

        }
        }
        class Mp3Filter implements FileFilter
        {
            public boolean accept(File file)
            {
            return (file.isDirectory()||file.getName().endsWith(".prop")|| file.getName().endsWith(".Mp3")||file.getName().endsWith(".wma")||file.getName().endsWith(".Wma")||file.getName().endsWith(".WMA")||file.getName().endsWith(".mp4"));
            }
        }
Was it helpful?

Solution

I think you're asking "how do I find the path to the file that was scanned by the Media Scanner and adding to the MediaStore content provider?"

The important column is MediaStore.MediaColumns.DATA. When a file is added to MediaStore, the file name including its path are stored in the DATA column. You should be able to go from the information you have about an image to the DATA column, read the file name, and from it get its path.

OTHER TIPS

This is the sort of thing you find in the .DATA column

/mnt/storage/Music/Audioslave/Audioslave-Live_In_Cuba/01-audioslave-set_it_off.mp3

/mnt/storage/Music/Audioslave/Audioslave-Live_In_Cuba/02-audioslave-your_time_has_come.mp3

/mnt/storage/Music/Audioslave/Audioslave-Live_In_Cuba/03-audioslave-like_a_stone.mp3

It holds the full path and the trackname. You can actually look at the database by pulling it off your avd. Save to you PC and open up with a PC based product such as RazorSQL. Once connected, you can run queries etc. It has helped me enormously

If you want to buy my app, visit Google Play and search for Playlist Manager or visit https://play.google.com/store/apps/details?id=com.flyingdutchman.playlistmanager&hl=en

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