0
我需要在根目錄下創建CAT_IMG文件夾,並在列表視圖中檢索它。但CAT_IMG文件夾不在根目錄中創建。我在清單文件中添加了權限。請幫我在根目錄下創建一個文件夾。如何在根目錄下創建一個文件夾Android
private void createDirectoryAndSaveFile(Bitmap imageToSave) {
File direct = new File(getApplicationContext().getFilesDir() + "/CAT_IMG");
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
String fileName = "fav" + timeStamp + ".JPG";
if (!direct.exists()) {
File wallpaperDirectory = new File("/CAT_IMG");
wallpaperDirectory.mkdir();
}
File file = new File(new File("/CAT_IMG"), fileName);
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
createDirectoryAndSaveFile(photo);
Log.e("URI", data.getExtras().get("data") + "");
}
}
代碼檢索它在列表視圖:
private void getImages() {
String[] filenames = new String[0];
File path = new File(getApplicationContext().getFilesDir() + "/CAT_IMG");// add here your folder name
if (path.exists()) {
filenames = path.list();
}
for (int i = 0; i < filenames.length; i++) {
photos.add(path.getPath() + "/" + filenames[i]);
Log.e("FAV_Images", photos.get(i));
Name.add(filenames[i]);
//Sno.add(i); }
}
}
我不認爲你可以在根目錄下創建一個目錄。沒有權限。 – Raptor
可以在根目錄下創建文件夾 – shwettha
您無法從SDK應用程序寫入內部存儲根目錄。 –