在存储android上找不到图片

时间:2013-04-08 15:11:52

标签: android file

我有一个代码可以在手机上查找图片。它适用于三星星系标签,但不适用于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;
}
}

唯一的区别是

我的平板电脑图片中的

位于/图片和手机中,位于/ Images

感谢您的帮助

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

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

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

如果(Folder.equal( “/图片”)){ ... } 如果{Folder.equal(“/ Images”)){ ... }