0

I'm trying to create a folder on the SD card that is named by user inputs, I get no errors when I run it, but it also doesn't create the folder.

the following is all the code I have written for this activity:

public class Jobselection extends AppCompatActivity
     implements OnClickListener {
     Button createButton;
     EditText photogname;
     EditText projnum;
     EditText phase;
     DatePicker datePicker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jobselection);
        createButton = (Button) findViewById(R.id.createButton);
        createButton.setOnClickListener(this);
        photogname = (EditText) findViewById(R.id.photographername);
        projnum = (EditText) findViewById(R.id.projectnumber);
        phase = (EditText) findViewById(R.id.phase);
        datePicker = (DatePicker) findViewById(R.id.datePicker);

    }

    public static java.util.Date getDateFromDatePicker(DatePicker datePicker) {
        int day = datePicker.getDayOfMonth();
        int month = datePicker.getMonth();
        int year = datePicker.getYear();
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);
        return calendar.getTime();
    }
    public void onClick(View createButton) {
    String date = getDateFromDatePicker(datePicker).toString();
    String photog = photogname.getText().toString();
    String proj = projnum.getText().toString() + "." + phase.getText().toString();
    String state;
    state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        File appDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + date + "/" + proj + "/" + photog);
        boolean isDirectoryCreated = appDirectory.exists();
        if (!isDirectoryCreated) {
            isDirectoryCreated = appDirectory.mkdirs();
        }
        if (isDirectoryCreated) {
            Toast.makeText(Jobselection.this, "Folder is created", Toast.LENGTH_LONG).show();
        }
        else
            Log.d("error","dir.already exists");
    }

    Intent launchUnitLoc = new Intent(this, UnitLocation.class);
    startActivity(launchUnitLoc);
}
Pang
  • 9,564
  • 146
  • 81
  • 122

6 Answers6

0

[Possible duplicate]

If you need to create files on the SDCard you can use publics directories like Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DCIM)

This answer is on this link

Gustavo Mora
  • 843
  • 1
  • 7
  • 18
0
if (!isDirectoryCreated) {
    isDirectoryCreated = appDirectory.mkdirs();

change this to:

if (!isDirectoryCreated) {
    appDirectory.mkdirs();

Hope this works

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Unfortunately not, it still doesn't create a folder on the SD card. – RedCaribou Dec 04 '17 at 23:19
  • I have posted all the code I wrote for the activity, please let me know if there's a particular part of the code you need to see and I will post it as well. – RedCaribou Dec 04 '17 at 23:34
  • You are not checking the return value of mkdirs(). So you do not know if it succeeded. Bad coding example. – greenapps Dec 05 '17 at 00:02
0

Sorry for making you wait, It appears you do not have code errors. Last thing you need to do is, check for the write permission add this block of code: state = Environment.getExternalStorageState();

    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            Log.v("Permisson", "Permission is granted");

        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
        }
    }
0

Instead of using this :-

File appDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + date + "/" + proj + "/" + photog);

Try this :-

File appDirectory = new File(Environment.getExternalStorageDirectory() + "/" + date + "/" + proj ,photog );
Suraj Nair
  • 1,729
  • 14
  • 14
0

I just modified your code, here is the full code

public class Jobselection extends AppCompatActivity
implements View.OnClickListener {
private static final int REQUEST_CODE = 200 ;
Button createButton;
EditText photogname;
EditText projnum;
EditText phase;
DatePicker datePicker;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jobselection);
    createButton = (Button) findViewById(R.id.createButton);
    createButton.setOnClickListener(this);
    photogname = (EditText) findViewById(R.id.editText);//added just to clear error
    projnum = (EditText) findViewById(R.id.editText);//added just to clear erro
    phase = (EditText) findViewById(R.id.editText);// added just to clear error
    datePicker = (DatePicker) findViewById(R.id.datePicker);

}

public static java.util.Date getDateFromDatePicker(DatePicker datePicker) {
    int day = datePicker.getDayOfMonth();
    int month = datePicker.getMonth();
    int year = datePicker.getYear();
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, day);
    return calendar.getTime();
}

@RequiresApi(api = Build.VERSION_CODES.M)
public void onClick(View createButton) {
    String date = getDateFromDatePicker(datePicker).toString();
    String photog = "FinalTest";//photogname.getText().toString();just to clear error

    String proj = projnum.getText().toString() + "." + phase.getText().toString();
    String state;
    state = Environment.getExternalStorageState();

    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            Log.v("Permisson", "Permission is granted");
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
        }
    }
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        File appDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + date + "/" + proj + "/" + photog);
        boolean isDirectoryCreated = appDirectory.exists();
        if (!isDirectoryCreated) {
            isDirectoryCreated = appDirectory.mkdirs();
        }
        if (isDirectoryCreated) {
            Toast.makeText(Jobselection.this, "Folder is created", Toast.LENGTH_LONG).show();
        } else
            Log.d("error", "dir.already exists");
    }
}
}
Intent launchUnitLoc = new Intent(this, UnitLocation.class);
startActivity(launchUnitLoc);

and your Manifest file should be like:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="[your package name]">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Jobselection">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

  • Hope this works :) Do not forget to allow permission when it asks for it :P – Dipak Poudel Dec 06 '17 at 01:25
  • I'm getting an error with requestPermissions where it says it cannot resolve the method, also for @RequiresApi it says it cannot resolve symbol RequiresApi and the same thing for .M Thank you for all your help so far I cannot tell you how grateful I am! – RedCaribou Dec 06 '17 at 17:45
0

you have to import that ;) when you copy and paste it doesn't import automatically. Make sure you have similar imports like this:

package [your package name];

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.util.Calendar;
//upto here
public class Jobselection extends AppCompatActivity
  • When I try importing RequiresApi it says is cannot resolve symbol RequiresApi...might this mean I need to update my android studio? – RedCaribou Dec 06 '17 at 18:34
  • Sorry I am not sure about that, last thing I can do is give you working project files. I uploaded my whole project here make necessary adjustments. link : https://www.datafilehost.com/d/04baa16e – Dipak Poudel Dec 06 '17 at 18:46
  • I'm honestly at a loss here, I'm using these things exactly as you, and the android developers have shown, and I keep getting errors. Most notably how it gives me an error with requestPermissions (it says cannot resolve method) – RedCaribou Dec 06 '17 at 19:47
  • Download and import and run the project I posted on last comment. If you cant run that, you have something wrong with your setup. – Dipak Poudel Dec 07 '17 at 00:18
  • I had to change the API's that my app would run on but this solution does work thank you ever so much for your help! Although it creates the file on the internal storage not the external SD card... – RedCaribou Dec 07 '17 at 00:19
  • I will say though it didn't create the folder on the SD card, not really sure why – RedCaribou Dec 07 '17 at 00:26