Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private String getSDcardPath()
- {
- String exts = Environment.getExternalStorageDirectory().getPath();
- String sdCardPath = null;
- try
- {
- FileReader fr = new FileReader(new File("/proc/mounts"));
- BufferedReader br = new BufferedReader(fr);
- String line;
- while((line = br.readLine())!=null)
- {
- if(line.contains("secure") || line.contains("asec"))
- continue;
- if(line.contains("fat"))
- {
- String[] pars = line.split("\s");
- if(pars.length<2)
- continue;
- if(pars[1].equals(exts))
- continue;
- sdCardPath =pars[1];
- break;
- }
- }
- fr.close();
- br.close();
- return sdCardPath;
- }
- catch (Exception e)
- {
- }
- return sdCardPath;
- }
- if (Build.VERSION.SDK_INT >= 23) {
- if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
- == PackageManager.PERMISSION_GRANTED) {
- Toast.makeText(this, "Permission is granted", Toast.LENGTH_LONG).show();
- foldername = "myFolder";
- filename = "test.txt";
- //Сохранение файла на карту SD:
- fullpath = getSDcardPath()
- + "/" + foldername
- + "/" + filename;
- SaveFile(fullpath, "Какой-то текст");
- } else {
- Toast.makeText(this, "Permission is revoked", Toast.LENGTH_LONG).show();
- ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
- }
- }
- public void SaveFile (String filePath, String FileContent)
- {
- //Создание объекта файла.
- File fhandle = new File(filePath);
- try
- {
- //Если нет директорий в пути, то они будут созданы:
- if (!fhandle.getParentFile().exists())
- fhandle.getParentFile().mkdirs();
- //Если файл существует, то он будет перезаписан:
- fhandle.createNewFile();
- FileOutputStream fOut = new FileOutputStream(fhandle);
- OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
- myOutWriter.write(FileContent);
- myOutWriter.close();
- fOut.close();
- }
- catch (IOException e)
- {
- Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
- //e.printStackTrace();
- //textInfo.setText("Path " + filePath + ", " + e.toString());
- }
- }
Add Comment
Please, Sign In to add comment