Playing Music From Sd Card Works On Emulator But Not On Phone
I'm creating an android application that plays the users music. I've got it to work fine on an emulator but it's not working when I install it on a phone, it crashes at this line:
Solution 1:
How about
return Environment.getExternalStorageDirectory().toString() + "/Music";
This will return the path to internal SD mount point like "/mnt/sdcard"
This is a better way of coding than hard coding in the path.
EDIT
To get it working on all devices try the code below from this thread where they discuss that Android has no concept of "external SD", aside from external storage. OP then came up with the following solution for his problem as a result of all the answers and comments he was given.
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import android.os.Environment;
import android.util.Log;
publicclassExternalStorage {
publicstaticfinalStringSD_CARD="sdCard";
publicstaticfinalStringEXTERNAL_SD_CARD="externalSdCard";
/**
* @return True if the external storage is available. False otherwise.
*/publicstaticbooleanisAvailable() {
Stringstate= Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
returntrue;
}
returnfalse;
}
publicstatic String getSdCardPath() {
return Environment.getExternalStorageDirectory().getPath() + "/";
}
/**
* @return True if the external storage is writable. False otherwise.
*/publicstaticbooleanisWritable() {
Stringstate= Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
returntrue;
}
returnfalse;
}
/**
* @return A map of all storage locations available
*/publicstatic Map<String, File> getAllStorageLocations() {
Map<String, File> map = newHashMap<String, File>(10);
List<String> mMounts = newArrayList<String>(10);
List<String> mVold = newArrayList<String>(10);
mMounts.add("/mnt/sdcard");
mVold.add("/mnt/sdcard");
try {
FilemountFile=newFile("/proc/mounts");
if(mountFile.exists()){
Scannerscanner=newScanner(mountFile);
while (scanner.hasNext()) {
Stringline= scanner.nextLine();
if (line.startsWith("/dev/block/vold/")) {
String[] lineElements = line.split(" ");
Stringelement= lineElements[1];
// don't add the default mount path// it's already in the list.if (!element.equals("/mnt/sdcard"))
mMounts.add(element);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
try {
FilevoldFile=newFile("/system/etc/vold.fstab");
if(voldFile.exists()){
Scannerscanner=newScanner(voldFile);
while (scanner.hasNext()) {
Stringline= scanner.nextLine();
if (line.startsWith("dev_mount")) {
String[] lineElements = line.split(" ");
Stringelement= lineElements[2];
if (element.contains(":"))
element = element.substring(0, element.indexOf(":"));
if (!element.equals("/mnt/sdcard"))
mVold.add(element);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
for (inti=0; i < mMounts.size(); i++) {
Stringmount= mMounts.get(i);
if (!mVold.contains(mount))
mMounts.remove(i--);
}
mVold.clear();
List<String> mountHash = newArrayList<String>(10);
for(String mount : mMounts){
Fileroot=newFile(mount);
if (root.exists() && root.isDirectory() && root.canWrite()) {
File[] list = root.listFiles();
Stringhash="[";
if(list!=null){
for(File f : list){
hash += f.getName().hashCode()+":"+f.length()+", ";
}
}
hash += "]";
if(!mountHash.contains(hash)){
Stringkey= SD_CARD + "_" + map.size();
if (map.size() == 0) {
key = SD_CARD;
} elseif (map.size() == 1) {
key = EXTERNAL_SD_CARD;
}
mountHash.add(hash);
map.put(key, root);
}
}
}
mMounts.clear();
if(map.isEmpty()){
map.put(SD_CARD, Environment.getExternalStorageDirectory());
}
return map;
}
}
USAGE
Map<String, File> externalLocations = ExternalStorage.getAllStorageLocations();
File sdCard = externalLocations.get(ExternalStorage.SD_CARD);
File externalSdCard = externalLocations.get(ExternalStorage.EXTERNAL_SD_CARD);
Solution 2:
I think the reason is , you have run the app connecting your phone to pc in debug mode. And your phone is in Mass Storage Mode , so when connected to pc your SD card is unmounted. That's why you are not getting the songs in your list. Hope it helps.
Post a Comment for "Playing Music From Sd Card Works On Emulator But Not On Phone"