2016-10-05 159 views
2

我知道這是重複的問題,但目前還沒有任何解決方案。我已經使用最新的API等級24與測試API 23.我一定要保存在SD卡上的圖像與我的應用程序名稱文件夾,但文件夾沒有創建...在Android的DCIM文件夾下的SD卡上創建文件夾的錯誤

我有下面的方法寫,但它不工作

public void actionScreenShot(View view) { 

     //enable drawing cache true 
     imageContainer.setDrawingCacheEnabled(true); 
     imageContainer.buildDrawingCache(true); 

     //create image from layout 
     Bitmap bitmap = Bitmap.createBitmap(imageContainer.getDrawingCache()); 


     //String folder_path = getApplicationContext().getFilesDir().getPath() + "/MyAppTest/"; //It's work properly 
     //String folder_path = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DCIM) + "/MyAppTest/"; //It's work properly 
     String folder_path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/MyAppTest"; //It's not work properly 
     if (checkFile(folder_path)) { 
      saveFile(new File(folder_path), bitmap); 
     } else { 
      File folder = new File(folder_path); 
      if (folder.mkdirs()) { 
       saveFile(folder, bitmap); 
      } else { 
       Log.d("Directory", " >> not created"); 
      } 

     } 
    } 

    public void saveFile(File folder, Bitmap bitmap) { 
     boolean success = false; 
     try { 
      File file = new File(folder, "temp.png"); 
      if (file.exists()) file.delete(); 
      FileOutputStream out = new FileOutputStream(file); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 
      out.flush(); 
      out.close(); 
      success = checkDatabase(file.getPath()); 
     }catch (Exception e) { 
      e.printStackTrace(); 
     } 

     if (success) { 
      Log.d("FILE", "success"); 
     } else { 
      Log.d("FILE", "not success"); 
     } 
    } 

    public boolean checkFile(String myPath) { 
     boolean isExist = false; 
     try { 
      File file = new File(myPath); 
      isExist = file.exists(); 
     } catch (Exception e) { 

     } 
     return isExist; 

    } 

權限也給出在AndroidManifest.xml

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

日誌跟蹤如下...

10-05 17:08:09.424 2808-2808/packagename D/Directory >> not created 

如果提供的解決方案,然後幫我...

感謝....

+0

'但它不工作'。請提供更好的信息。你應該開始告訴你文件夾是否被創建。 – greenapps

+0

如果mkdirs()失敗,則不通知用戶。在那裏舉杯祝酒。您應該使用更多日誌和吐司聲明,以便您可以告訴我們發生了什麼,發生了什麼以及看到了什麼。 – greenapps

+0

發佈您的堆棧跟蹤。關於運行時間權限呢? –

回答

1

這裏是簡單的代碼在SD卡創建的文件夾清單文件

// create a File object for the parent directory 
File newFolder = new File("/sdcard/newFolder/"); 
// have the object build the directory structure, if needed. 
newFolder.mkdirs(); 
// create a File object for the output file 
File outputFile = new File(newFolder, filename); 
// now attach the OutputStream to the file object, instead of a String   
FileOutputStream fos = new FileOutputStream(outputFile); 

許可

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

此代碼也嘗試,但它不起作用... –

+0

它在這裏工作。在這裏發佈錯誤 –

+0

'newFolder.mkdirs();'。您應該檢查返回值,因爲mkdirs可能會失敗。爲用戶顯示敬酒信息。然後返回。那麼不要繼續使用代碼。 – greenapps

0

問題是你使用API​​ 23和24(棉花糖)。 Google從API 23中更改了許可策略。 將您的目標版本降低爲22.它將起作用。如果你想要23或24,那麼包含代碼以在運行時請求許可。