0

I am trying to simply save a File to my Device. I added the Permission in the Manifest and the app does ask me for permission to access the storage but when I click "Save File" my "permission failed" Toast pops up. Maybe I don't ask for permission correctly. I tried it on my Samsung S10 and on the emulator and on both devices it does not work. If I try to use it on my real device a second time, the app just crashes. However, that does not happen on the emulator Hope someone can help me. Thanks already.

////code

package com.example.writeexeternalstorage;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Permission;

public class MainActivity extends AppCompatActivity {

    EditText fileName;
    EditText text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Here, thisActivity is the current activity

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
        }
        setContentView(R.layout.activity_main);



        setContentView(R.layout.activity_main);
    }

    private boolean isExternalStorageWriteable(){
        if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
            return true;
        }else{
            return false;
        }
    }

    public void writeFile(View v){
        if(isExternalStorageWriteable() && checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            File textFile = new File(Environment.getExternalStorageDirectory(), fileName.toString());

            try {
                FileOutputStream fos = new FileOutputStream(textFile);
                fos.write(text.getText().toString().getBytes());
                fos.close();

                Toast.makeText(this, "File saved", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }else if(isExternalStorageWriteable() == false)
        {
            Toast.makeText(this, "External Storage is not writable",Toast.LENGTH_SHORT).show();
        }

        else if(checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == false) {
            Toast.makeText(this, "Permission failed", Toast.LENGTH_SHORT).show();
        }

    }

    public boolean checkPermission(String permission){
        int check = ContextCompat.checkSelfPermission(this,permission);
        return (check == PackageManager.PERMISSION_GRANTED);
    }
}
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
ApflaBua
  • 15
  • 5
  • Please tell full path of the file you try to write to. And Android version of devices used. – blackapps Nov 03 '20 at 12:00
  • Place also a Toast in that catch block. – blackapps Nov 03 '20 at 12:02
  • i dont have any specific path to write to. this is simply to learn how the interaction with external storage works because i was not able to read anything from a file on my sdcard. my phone has Android 10 – ApflaBua Nov 03 '20 at 12:32
  • Of course there is a specific path that you use. I sse it and i could tell you but would rather see you telling me. And... it has nothing to do with a removable micro sd card your path. – blackapps Nov 03 '20 at 12:33
  • I honestly dont quite understand how "Environment.getExternalStorageDirectory()" works but it since i didnt add any other directory or folder to put my file in it will just save the, for example .txt file onto the card (or the internal storage because the internal storage is declared as sdcard in android studio) but i was trying to figure out wich one. – ApflaBua Nov 03 '20 at 12:39
  • Have a look at string `Environment.getExternalStorageDirectory().getAbsolutePath()` and you will see the path i asked you for. – blackapps Nov 03 '20 at 12:48
  • Yeah I mean its going to be in root directory of my external storage – ApflaBua Nov 03 '20 at 13:06
  • Hmmm... can be... But what i meant is that you would tell us the value of that path. Which full path is it? You still did not tell. – blackapps Nov 03 '20 at 13:07
  • I have honestly no idea what are you talking about i am very sorry – ApflaBua Nov 03 '20 at 13:14
  • `Toast.makeText(.... Environment.getExternalStorageDirectory().getAbsolutePath() ...).show();` Which value do you see? – blackapps Nov 03 '20 at 15:51
  • "/storage/emulated/0" and now i am really confused becaus I have no idea what that is – ApflaBua Nov 04 '20 at 09:18

1 Answers1

0

if you are using Environment.getExternalStorageDirectory() on an API level greater than 29 then it would not work, because starting from API level 29 android made this function deprecated. Hope this would answer your question.

Hiraeths
  • 380
  • 3
  • 16
Taha alam
  • 372
  • 3
  • 11