javafileandroid-studiomkdirmkdirs

mkdir() is not creating a new directory


I have included

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

and I want to save image.

But before that I wanted to check that is it creating directory or not and it is not. I am using File Explorer to check that is it creating or not and it is even not showing me my app package folder.

package com.example.parth.project2;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button; 
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;

public class saveData extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_save_data);

       ImageView imageView = (ImageView) findViewById(R.id.imagesave);
       Button button = (Button) findViewById(R.id.Savepic);


       button.setOnClickListener(new View.OnClickListener() {
          @Override
           public void onClick(View v) {
               String filepath = Environment.getExternalStorageDirectory().getPath();
               File fileBase = new File(filepath, "Test");
               if (!fileBase.exists()) {
                   fileBase.mkdirs();
                   Toast.makeText(saveData.this, fileBase.getAbsolutePath(), Toast.LENGTH_LONG).show();
               }
           }
       });

   }

}

Solution

  • Try :

    String filepath = Environment.getExternalStorageDirectory().toString();
    
    //or
    
    String filepath = Environment.getExternalStorageDirectory();
    

    Instead of :

    String filepath = Environment.getExternalStorageDirectory().getPath();