0

我有一个代码可以在手机上查找图片。它适用于三星银河选项卡,但不适用于 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 中

谢谢你的帮助

4

2 回答 2

2

那是因为在 HTC Wildfire 上

"/Images" != Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)

通常要获取图像,您最好使用 ContentProvider:http: //developer.android.com/guide/topics/providers/content-provider-basics.html

像这样的东西:

ContentResolver cr = mContext.getContentResolver();
Cursor cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
于 2013-04-08T15:24:08.330 回答
0

您是否添加了读取 sd 卡的权限?

但在您的代码中,检查文件:

if(Folder.equal("/pictures")){ ... } else if{Folder.equal("/Images")){ ... }

于 2013-04-08T15:25:37.313 回答