2016-10-05 157 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,那么包含代码以在运行时请求许可。