2017-08-29 565 views
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);  } 
    } 

} 
+0

我不認爲你可以在根目錄下創建一個目錄。沒有權限。 – Raptor

+0

可以在根目錄下創建文件夾 – shwettha

+0

您無法從SDK應用程序寫入內部存儲根目錄。 –

回答

-1

我創造了我上面的代碼做微小變化在根目錄下的一個文件夾
`

private void createDirectoryAndSaveFile(Bitmap imageToSave) { 

     File direct = new File(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"); 
      direct.mkdir(); 
     } 

     File file = new File(direct, 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") + ""); 

     } 
    }` 
+0

您不是在根目錄中創建文件夾,而是在您的應用程序文件夾中創建它,它們是不同的。更多信息:https://developer.android.com/reference/android/content/Context.html#getFilesDir()正如別人在評論中所說的,默認情況下你不能觸摸根文件夾中的任何東西。 –

相關問題