I have a question now i try create a music app on my phone. now I can see my file song But the name shown in the app is the file address instead. and if i want to play the file music in this app. What code do I need to add for get the name of music and play it on my phone? thank a lot
JavaScript
x
26
26
1
@Override
2
public void onClick(View view) {
3
mp.pause();
4
play.setEnabled(true);
5
stop.setEnabled(true);
6
pause.setEnabled(false);
7
}
8
});
9
stop.setOnClickListener(new View.OnClickListener() {
10
@Override
11
public void onClick(View view) {
12
if (mp != null) {
13
mp.stop();
14
try {
15
mp.prepare();
16
} catch (IOException e) {
17
e.printStackTrace();
18
}
19
//mp.release()
20
play.setEnabled(true);
21
play.setEnabled(true);
22
stop.setEnabled(false);
23
}
24
}
25
});
26
send.setOnClickListener(new View.OnClickListener() {
JavaScript
1
25
25
1
//List<Intent> intentShareList = new ArrayList<Intent>();
2
@Override
3
public void onClick( View view ) {
4
msgs = msg.getText().toString();
5
System.out.print("msgs " + msgs);
6
// make line message
7
Log.d(TAG, "Txt " + msgs);
8
Intent shareIntent = new Intent();
9
String userId = "";
10
String sendText = "line://ti/p/~" + userId;
11
//shareIntent = null;
12
try {
13
shareIntent = Intent.parseUri(sendText,
14
Intent.URI_INTENT_SCHEME);
15
} catch (URISyntaxException e) {
16
e.printStackTrace();
17
}
18
shareIntent.setAction(Intent.ACTION_SEND);
19
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
20
shareIntent.putExtra(Intent.EXTRA_TEXT,msgs);
21
shareIntent.setType("text/plain");
22
startActivity(shareIntent);
23
}
24
});
25
} AND
JavaScript
1
17
17
1
public class Songs {
2
//private
3
private String songTitle;
4
private String songAddress;
5
public Songs(String title){
6
//songID = id;
7
songTitle = title;
8
9
}
10
/*public long getSongID(){
11
return songId;
12
*/
13
public String getSongTitle(){
14
15
return songTitle;
16
}
17
}
Advertisement
Answer
now I can see my file song But the name shown in the app is the file address instead
I think you are referring to the “Path” of the file, for example:
/storage/emulated/0/song.mp3 (Unix style path)
C:Songssong.mp3 (Window style path)
If you would like to parse (find out) the name of the file, use the following code:
JavaScript
1
5
1
String filePath = "/storage/emulated/0/song.mp3";
2
File f = new File(filePath);
3
String fileName = f.getName();
4
// fileName == "song.mp3"
5
In your case:
JavaScript
1
26
26
1
public class Songs {
2
private String title; // renamed from "songTitle"
3
private String path; // renamed from "songAddress"
4
private String fileName; // the file name of the song
5
6
public Songs(String title, String path){
7
this.title = title;
8
this.path = path;
9
File file = new File(path);
10
String fileName = f.getName();
11
this.fileName = fileName;
12
}
13
14
public String getTitle(){
15
return title;
16
}
17
18
public String getPath(){
19
return path;
20
}
21
22
public String getFileName(){
23
return fileName;
24
}
25
}
26