我想在内部存储器中创建一个画廊文件夹,如下面的屏幕截图所示:
单击相册时,我希望它显示其中的图像列表。
String dirPath = getFilesDir().getAbsolutePath() + File.separator + "newfoldername";
File projDir = new File(dirPath);
我想在内部存储器中创建一个画廊文件夹,如下面的屏幕截图所示:
单击相册时,我希望它显示其中的图像列表。
String dirPath = getFilesDir().getAbsolutePath() + File.separator + "newfoldername";
File projDir = new File(dirPath);
为了为您的应用程序创建一个文件夹,下面是代码,它采用您的文件夹的字符串名称并检查 SD 卡是否可用,如果它在图片文件夹中创建该名称的文件夹,否则如果 Sd 卡不存在则创建文件夹安装应用程序的内部存储器中的相同名称,例如 data/data.com.myapp/files/yourFolder
public File createDirectoryForApp(Context con, String dirName)
{
File directory=null;
/*
* This sections checks the phone to see if there is a SD card. if
* there is an SD card, a directory is created on the SD card
*/
if (isExternalStorageWritable()) {
// search for directory on SD card
directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/"+dirName+"/");
// if no directory exists, create new directory to store test
// results
if (!directory.exists()) {
directory.mkdir();
}
}
else
{
//create new file directory object in internal memory as no SD card is available
directory = new File(con.getFilesDir()
+ "/"+dirName+"/");
// if no directory exists, create new directory
if (!directory.exists()) {
directory.mkdir();
}
}
return directory;
}
//method used above to check external storage
public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
并获取具有特定扩展名的文件和内部目录中的其他文件(也是文件),您可以使用以下方法
private List<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<File>();
File[] files = parentDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
inFiles.addAll(getListFiles(file));
} else {
if(file.getName().endsWith(".png")){
inFiles.add(file);
}
}
}
return inFiles;
}
并且要直观地表示所有这些信息,这取决于您如何设计布局并在从这些文件中获取数据后显示数据。