没有获得gridview图像的正确路径

时间:2014-01-10 05:17:06

标签: java android gridview

我正在开发一个应用程序,其中我有gridview但不知何故我的应用程序没有获得正确的图像路径。 我想从gridview 中的drawable文件夹中选择图片。模拟器显示“NAT目录路径无效”等错误

以下是AppConstant.java的代码

public class AppConstant {

// Number of columns of Grid View
public static final int NUM_OF_COLUMNS = 3;

// Gridview image padding
public static final int GRID_PADDING = 8; // in dp

// SD card image directory
public static final String PHOTO_ALBUM = "NAT";

// supported file formats
public static final List<String> FILE_EXTN = Arrays.asList("jpg", "jpeg",
        "png");

}

这是Utils.java的代码

public class Utils {

private Context _context;

// constructor
public Utils(Context context) {
    this._context = context;
}

/*
 * Reading file paths from SDCard
 */
public ArrayList<String> getFilePaths() {
    ArrayList<String> filePaths = new ArrayList<String>();

    File directory = new File(
            android.os.Environment.getExternalStorageDirectory()
                    + File.separator + AppConstant.PHOTO_ALBUM);

    // check for directory
    if (directory.isDirectory()) {
        // getting list of file paths
        File[] listFiles = directory.listFiles();

        // Check for count
        if (listFiles.length > 0) {

            // loop through all files
            for (int i = 0; i < listFiles.length; i++) {

                // get file path
                String filePath = listFiles[i].getAbsolutePath();

                // check for supported file extension
                if (IsSupportedFile(filePath)) {
                    // Add image path to array list
                    filePaths.add(filePath);
                }
            }
        } else {
            // image directory is empty
            Toast.makeText(
                    _context,
                    AppConstant.PHOTO_ALBUM
                            + " is empty. Please load some images in it !",
                    Toast.LENGTH_LONG).show();
        }

    } else {
        AlertDialog.Builder alert = new AlertDialog.Builder(_context);
        alert.setTitle("Error!");
        alert.setMessage(AppConstant.PHOTO_ALBUM
                + " directory path is not valid! Please set the image directory name AppConstant.java class");
        alert.setPositiveButton("OK", null);
        alert.show();
    }

    return filePaths;
}

/*
 * Check supported file extensions
 * 
 * @returns boolean
 */
private boolean IsSupportedFile(String filePath) {
    String ext = filePath.substring((filePath.lastIndexOf(".") + 1),
            filePath.length());

    if (AppConstant.FILE_EXTN
            .contains(ext.toLowerCase(Locale.getDefault())))
        return true;
    else
        return false;

}

/*
 * getting screen width
 */
public int getScreenWidth() {
    int columnWidth;
    WindowManager wm = (WindowManager) _context
            .getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    final Point point = new Point();
    try {
        display.getSize(point);
    } catch (java.lang.NoSuchMethodError ignore) { // Older device
        point.x = display.getWidth();
        point.y = display.getHeight();
    }
    columnWidth = point.x;
    return columnWidth;
}

}

2 个答案:

答案 0 :(得分:1)

这可能是由于你的文件夹NAT有一些子文件夹而且图像存储在该子文件夹中。

所以使用:public static final String PHOTO_ALBUM =“NAT / Subfolder”;

答案 1 :(得分:0)

I just want to retrieve images from drawable folder

您可以执行以下操作

int[] images= {R.drawable.image1,R.drawable.image2};

现在使用此int[] images填充gridview。