Android開發本地及網路Mp3音樂播放器(十八)新下載歌曲載入準備SdcardSearchToList

來源:互聯網
上載者:User

標籤:

掃描本地檔案載入到item

不限於應用到音樂播放器,其他Android工程,也可以使用SdcardSearchToList

其他Android工程也可以使用,我現在做了很小的Demo

後續將博文,將在播放器裡,加入這一功能,來實現剛下載好的音樂,載入到播放器

這個Demo的源碼下載:
http://download.csdn.net/album/detail/3105
名稱為:SdcardSearchToList,如果沒有顯示,可能還沒有審核完成
不下載也可以,因為只有1個java檔案,2個xml檔案,非常小

截止到目前的源碼下載:
http://download.csdn.net/album/detail/3105
(最新的,請下載最後一個,本博文對應版本2.1;如果需要逐步實現的過程,請下載所有)

實現效果



實現代碼如下:

GetListActivity如下:(主Activity)

package com.iwanghang.sdcardsearchtolist;import android.os.Bundle;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ListView;import android.widget.SimpleAdapter;/** * 不要忘記加許可權 * <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> */public class GetListActivity extends Activity {    private Button myBtn = null;    private ListView lv = null;    private SimpleAdapter sa = null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_get_list);        lv = (ListView)findViewById(R.id.listv);        String[] from = {"text_content"};        int[] to = {R.id.myView1};        sa = new SimpleAdapter(this, getList(), R.layout.itemlist, from, to);        myBtn = (Button)findViewById(R.id.myButton);        myBtn.setOnClickListener(new OnClickListener() {            public void onClick(View arg0) {                // TODO Auto-generated method stub                lv.setAdapter(sa);            }        });    }    public List<Map<String, Object>> getList()    {        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();        String sDStateString = Environment.getExternalStorageState();        if(sDStateString.equals(Environment.MEDIA_MOUNTED))        {            //File SDFile = Environment.getExternalStorageDirectory();            //File sdPath = new File(SDFile.getAbsolutePath());            File sdPath = new File(Environment.getExternalStorageDirectory() + "/drm_music");            System.out.println("GetListActivity.List.sdPath = " + sdPath );            System.out.println("GetListActivity.List.listFiles = " + sdPath.listFiles() );            if(sdPath.listFiles().length > 0)            {                for(File file : sdPath.listFiles())                {                    Map<String, Object> map = new HashMap<String, Object>();                    map.put("text_content", file.getName());                    list.add(map);                    System.out.println("name-->"+file.getName());                }            }        }        return list;    }    //其中,getList()用於掃描本地檔案,獲得檔案的名字;為ListView設定一個適配器,將掃描到的檔案顯示在ListView上,這裡需要注意,適配器的後面兩個參數都是數組。}

activity_get_list.xml如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.iwanghang.sdcardsearchtolist.GetListActivity"    android:orientation="vertical">    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="擷取本地檔案資訊" />    <Button        android:id="@+id/myButton"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="掃描" />    <ListView        android:id="@+id/listv"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:scrollbars="vertical" /></LinearLayout>

itemlist.xml如下:

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/lvitem"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:stretchColumns ="*">    <TableRow>        <TextView            android:id="@+id/myView1"            android:layout_width="wrap_content"            android:layout_height="wrap_content">        </TextView>        <TextView            android:id="@+id/myView2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            >        </TextView>        <TextView            android:id="@+id/myView3"            android:layout_width="wrap_content"            android:layout_height="wrap_content">        </TextView>    </TableRow></TableLayout>


Android開發本地及網路Mp3音樂播放器(十八)新下載歌曲載入準備SdcardSearchToList

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.