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);
}
[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
if (!isDirectoryCreated) {
isDirectoryCreated = appDirectory.mkdirs();
change this to:
if (!isDirectoryCreated) {
appDirectory.mkdirs();
Hope this works
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);
}
}
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 );
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>
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.