Ekta
Ekta

Reputation: 438

Android Logging: Folder not created in /storage/emulated/0

I'm trying to create a log file in my android application using java.util.logging but the folder is not getting created. I don't understand the problem. Below is the code

public class MainActivity extends AppCompatActivity {

    private static final Logger logger = Logger.getLogger(MainActivity.class.getName());

    static  {
       try {
           File storagePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "TestApp");
           storagePath.mkdirs();

           boolean append = true;
           FileHandler fh = new FileHandler(storagePath + File.separator + "TestLog.log", append);
           fh.setFormatter(new SimpleFormatter());
           logger.addHandler(fh);
       } catch (IOException e) {
           e.printStackTrace();
       }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        logger.severe("my severe message");
        logger.warning("my warning message");
        logger.info("my info message");

    }
}

Any help will be appreciated.

Upvotes: 0

Views: 7700

Answers (1)

Ekta
Ekta

Reputation: 438

I found the problem. I'm using Android Marshmallow for testing. It requires the user to grant the permission to write into the external storage directory.

This link is very helpful: http://metova.com/dev/blog/2015/09/15/android-marshmallow-permissions/

This is a working code with tutorial: http://www.learn2crack.com/2015/10/android-marshmallow-permissions.html

Thanks everyone for the comments.

Upvotes: 0

Related Questions