Android : Android (android.os.Environment.getExternalStorageDirectory().getAbsolutePath())

on Tuesday, July 8, 2014


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


0 comments:

Post a Comment