如何从mediastore查询文件路径?或者如何从mediastore查询艺术家图像?

时间:2013-09-24 17:37:07

标签: android android-mediaplayer mediastore

我是mediastore.i的新手。我正在制作一个Android应用程序,我想知道如何从mediastore获取文件路径?还是艺术家形象? 我之前使用的是mediametadataretriever而不是mediastore,我正在组建自己的数据库,它工作正常,但它曾经滞后3秒,没有人想要那样。 下面是与我提出的问题无关的代码,但我发布的原因仅仅是因为如果没有发布代码,人们就会习惯于投票。  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"));
            }
        }

2 个答案:

答案 0 :(得分:2)

我想你在问“如何找到Media Scanner扫描并添加到MediaStore内容提供商的文件的路径?”

重要的列是MediaStore.MediaColumns.DATA。将文件添加到MediaStore时,包含其路径的文件名将存储在DATA列中。您应该可以从有关图像的信息转到DATA列,读取文件名,然后从中获取其路径。

答案 1 :(得分:1)

这是你在.DATA专栏

中找到的那种东西

/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

它包含完整路径和轨道名称。 您可以通过将其从avd中拉出来实际查看数据库。保存到PC并使用基于PC的产品(如RazorSQL)打开。连接后,您可以运行查询等。这对我帮助很大

如果您想购买我的应用,请访问Google Play并搜索播放列表管理器 或访问 https://play.google.com/store/apps/details?id=com.flyingdutchman.playlistmanager&hl=en