kathleen55
kathleen55

Reputation: 341

app cannot find directory of images inside SD Card

Hello guys i am creating an app that will display images in gridview but the problem is the app cannot find the directory of the images inside the SDcard.

here's the code for that.

public class Utils {

private Context _context;

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

public ArrayList<String> getFilePaths() {
    ArrayList<String> filePaths = new ArrayList<String>();

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

    if(directory.isDirectory()) {
        File[] listFiles = directory.listFiles();

    if(listFiles.length > 0) {

        for(int i = 0; i < listFiles.length; i++) {

            String filePath = listFiles[i].getAbsolutePath();

            if(IsSupportedFile(filePath)) {
                filePaths.add(filePath);
            }

            }

        }
    else {
        Toast.makeText(_context, AppConstant.PHOTO_ALBUM + "is empty. Please load some images", 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!");
        alert.setPositiveButton("OK", null);
        alert.show();
    }
    return filePaths;
}

private boolean IsSupportedFile(String filePath) {
    String ext = filePath.substring((filePath.lastIndexOf(".") + 1), filePath.length());

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

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) {
        point.x = display.getWidth();
        point.y = display.getHeight();
    }

    columnWidth = point.x;
    return columnWidth;
}

hopefully you can help me with this guys.. i dont know how will i access the SDcard through the use of that function

Upvotes: 0

Views: 155

Answers (2)

Anil Jadhav
Anil Jadhav

Reputation: 2158

To write to external storage, you must have to declare this in the manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Upvotes: 0

Alexandre Santos
Alexandre Santos

Reputation: 8338

There are no issues that I can see with your code. Just make sure that the user has given the app permission on the manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Upvotes: 1

Related Questions