我有一个代码可以在手机上查找图片。它适用于三星银河选项卡,但不适用于 htc 野火。
public class MyStorage extends Activity{
public static boolean mExternalStorageAvailable;
public static boolean mExternalStorageWriteable;
public static void checkAvailability(){
//Checking media availability
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
}
public static ArrayList<String> getFiles(){
checkAvailability();
ArrayList<String> result = new ArrayList<String>();
if(mExternalStorageAvailable){
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String basePath = folder.toString();
result = listDirectory(folder, basePath);
}
return result;
}
private static ArrayList<String> listDirectory(File folder, String basePath){
String[] list = folder.list();
ArrayList<String> result = new ArrayList<String>();
if(list != null){
for (int i = 0; i < list.length; i++) {
File test = new File(basePath+"/"+list[i]);
if(test.isDirectory()){
result.addAll(listDirectory(test,basePath+"/"+list[i]));
}else{
result.add(basePath+"/"+list[i]);
}
}
}
return result;
}
}
唯一的区别是
在我的平板电脑中,图片在 /Pictures 中,而在我的手机中,它们在 /Images 中
谢谢你的帮助