Hello.

package com.elf_m.android.testsdcard; import android.Manifest; import android.content.pm.PackageManager; import android.os.Environment; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import android.support.design.widget.Snackbar; public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback { Button mButton; private View mLayout; private static final int PERMISSION_STORAGE = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLayout = findViewById(R.id.main_layout); mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SaveFile(); } }); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { // BEGIN_INCLUDE(onRequestPermissionsResult) if (requestCode == PERMISSION_STORAGE) { // Request for camera permission. if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Permission has been granted. Start camera preview Activity. Toast.makeText(this, "WRITE_EXTERNAL_STORAGE. Starting SaveFile", Toast.LENGTH_SHORT).show(); SaveFileToSD(); } else { // Permission request was denied. Toast.makeText(this, "WRITE_EXTERNAL_STORAGE permission request was denied.", Toast.LENGTH_SHORT).show(); } } // END_INCLUDE(onRequestPermissionsResult) } private void SaveFile() { // BEGIN_INCLUDE(startCamera) // Check if the Camera permission has been granted if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // Permission is already available, start camera preview Toast.makeText(this, "WRITE_EXTERNAL_STORAGE is available.", Toast.LENGTH_SHORT).show(); SaveFileToSD(); } else { // Permission is missing and must be requested. requestWRITEEXTERNALPermission(); } // END_INCLUDE(startCamera) } private void requestWRITEEXTERNALPermission() { // Permission has not been granted and must be requested. if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { // Provide an additional rationale to the user if the permission was not granted // and the user would benefit from additional context for the use of the permission. // Display a SnackBar with a button to request the missing permission. Snackbar.make(mLayout, "WRITE_EXTERNAL_STORAGE access is required to display the camera preview.", Snackbar.LENGTH_INDEFINITE).setAction("OK", new View.OnClickListener() { @Override public void onClick(View view) { // Request the permission ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_STORAGE); } }).show(); } else { Snackbar.make(mLayout, "Permission is not available. Requesting WRITE_EXTERNAL_STORAGE permission.", Snackbar.LENGTH_SHORT).show(); // Request the permission. The result will be received in onRequestPermissionResult(). ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_STORAGE); } } public void SaveFileToSD() { try { File fhandle = new File(getSDcardPath().toString() + "/myfolder111/test.txt"); //Если Π½Π΅Ρ‚ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΉ Π² ΠΏΡƒΡ‚ΠΈ, Ρ‚ΠΎ ΠΎΠ½ΠΈ Π±ΡƒΠ΄ΡƒΡ‚ созданы: if (!fhandle.exists()) if(!fhandle.mkdirs()) Log.d("test", "Ошибка создания ΠΏΠ°ΠΏΠΊΠΈ!"); //Если Ρ„Π°ΠΉΠ» сущСствуСт, Ρ‚ΠΎ ΠΎΠ½ Π±ΡƒΠ΄Π΅Ρ‚ пСрСзаписан: if(!fhandle.createNewFile()) Log.d("test", "Ошибка создания Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚Π°"); FileOutputStream fOut = new FileOutputStream(fhandle); OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); myOutWriter.write("TEST"); myOutWriter.close(); fOut.close(); } catch (IOException e) { Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show(); } } 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; } } 
  • The second day I already fight, maybe you need additional permissions to write to the SD card? - Nick ELF-M
  • And in the manifest they wrote <uses-permission android: name = "android.permission.WRITE_EXTERNAL_STORAGE" />? - Ivan
  • Yes, of course I have, and I get it extra. USB connection from the tablet is not. - Nick ELF-M
  • Look all the way off in the developer.android.com/training/permissions/requesting.html#java documentation, when I did everything as it is written, I had no problems. - Ivan
  • Rewrote, did everything according to the documentation. All the same when writing a file: permission denied. It feels like something else is needed to write to an SD card - Nick ELF-M

1 answer 1

Looked at the code.

  if (pars[1].equals(exts)) continue; sdCardPath = pars[1]; Log.d("SaveFileToSD", "pars " + pars[1]); Log.d("SaveFileToSD", "pars " + pars[0]); break; 

In the place that I gave above sdCardPath = pars[1]; is not called because its condition is not satisfied and null is returned. Also about proc / mounts, there is no mounts folder on more than one of my devices, plus you are trying to write files to the root directory (we get permission denied, this requires root) Ошибка создания папки! null/myfolder111/test.txt Ошибка создания папки! null/myfolder111/test.txt , as I said, getSDcardPath returns nothing. I do not know how much I helped, but the code as promised looked.

  • Thank you so much! On my mouth-wah, everywhere the way came back. The question then remains, how to write files to the SD card, if the API is larger than KitKat. I looked through ADB, really on all the folders of the SD card of the root rights. Moreover, I specifically create a folder on my computer, connecting an SD card to it - Nick ELF-M
  • Can anyone have a ready piece of code to write files to the SD card? HELP !!! I need a damn SQLite database to move there and from there to work with it and it’s already the third day - Nick ELF-M