-1

我是 mediastore 的新手。我正在制作一个 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"));
            }
        }
4

2 回答 2

2

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

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

于 2013-09-24T17:56:12.407 回答
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

于 2013-09-25T19:34:20.383 回答