Android development Local and network MP3 Music Player (18) new download song Loading preparation sdcardsearchtolist

Source: Internet
Author: User


Scan local file to load to item

Not limited to apps to music player, other Android works, can also use Sdcardsearchtolist

Other Android projects can also be used, I have now done a very small demo

Later will post, will be in the player, add this function, to achieve just downloaded music, loaded into the player

This demo source download:
http://download.csdn.net/album/detail/3105
The name is: Sdcardsearchtolist, if not displayed, may not have been audited completed
Not download can also, because only 1 Java files, 2 XML files, very small

Until the current source download:
http://download.csdn.net/album/detail/3105
(Latest, please download the last one, this post corresponds to version 2.1; If you need to step through the process, please download all)

Achieve results








The implementation code is as follows:



Getlistactivity as follows: (main 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;

/ **
 * Don't forget to add permissions
 * <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;
    }

    // Among them, getList () is used to scan local files to obtain the file name; set an adapter for ListView and display the scanned files on ListView. It should be noted here that the last two parameters of the adapter are arrays.
}

activity_get_list.xml is as follows:

<? 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 = "Get local file information" />

    <Button
        android: id = "@ + id / myButton"
        android: layout_width = "fill_parent"
        android: layout_height = "wrap_content"
        android: text = "Scan" />
    <ListView
        android: id = "@ + id / listv"
        android: layout_width = "fill_parent"
        android: layout_height = "wrap_content"
        android: scrollbars = "vertical" />


</ LinearLayout>

itemlist.xml is as follows:

<? 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 development local and network Mp3 music player (eighteen) new download song loading preparation SdcardSearchToList

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.