Guest User

Untitled

a guest
Jan 21st, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. private String getSDcardPath()
  2. {
  3. String exts = Environment.getExternalStorageDirectory().getPath();
  4. String sdCardPath = null;
  5. try
  6. {
  7. FileReader fr = new FileReader(new File("/proc/mounts"));
  8. BufferedReader br = new BufferedReader(fr);
  9. String line;
  10. while((line = br.readLine())!=null)
  11. {
  12. if(line.contains("secure") || line.contains("asec"))
  13. continue;
  14. if(line.contains("fat"))
  15. {
  16. String[] pars = line.split("\s");
  17. if(pars.length<2)
  18. continue;
  19. if(pars[1].equals(exts))
  20. continue;
  21. sdCardPath =pars[1];
  22. break;
  23. }
  24. }
  25. fr.close();
  26. br.close();
  27. return sdCardPath;
  28. }
  29. catch (Exception e)
  30. {
  31. }
  32. return sdCardPath;
  33. }
  34.  
  35. if (Build.VERSION.SDK_INT >= 23) {
  36. if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
  37. == PackageManager.PERMISSION_GRANTED) {
  38. Toast.makeText(this, "Permission is granted", Toast.LENGTH_LONG).show();
  39.  
  40. foldername = "myFolder";
  41. filename = "test.txt";
  42. //Сохранение файла на карту SD:
  43. fullpath = getSDcardPath()
  44. + "/" + foldername
  45. + "/" + filename;
  46. SaveFile(fullpath, "Какой-то текст");
  47.  
  48. } else {
  49. Toast.makeText(this, "Permission is revoked", Toast.LENGTH_LONG).show();
  50. ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
  51. }
  52. }
  53.  
  54. public void SaveFile (String filePath, String FileContent)
  55. {
  56. //Создание объекта файла.
  57. File fhandle = new File(filePath);
  58. try
  59. {
  60. //Если нет директорий в пути, то они будут созданы:
  61. if (!fhandle.getParentFile().exists())
  62. fhandle.getParentFile().mkdirs();
  63. //Если файл существует, то он будет перезаписан:
  64. fhandle.createNewFile();
  65. FileOutputStream fOut = new FileOutputStream(fhandle);
  66. OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
  67. myOutWriter.write(FileContent);
  68. myOutWriter.close();
  69. fOut.close();
  70. }
  71. catch (IOException e)
  72. {
  73. Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
  74. //e.printStackTrace();
  75. //textInfo.setText("Path " + filePath + ", " + e.toString());
  76. }
  77. }
Add Comment
Please, Sign In to add comment