我试图在SD卡上创建一个由用户输入命名的文件夹,运行时我没有错误,但它也没有创建文件夹。
以下是我为此活动编写的所有代码:
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);
}
答案 0 :(得分:0)
[可能重复]
如果您需要在SDCard上创建文件,可以使用公共目录,例如Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DCIM)
这个答案就在link
上答案 1 :(得分:0)
if (!isDirectoryCreated) {
isDirectoryCreated = appDirectory.mkdirs();
将其更改为:
if (!isDirectoryCreated) {
appDirectory.mkdirs();
希望这有效
答案 2 :(得分:0)
很抱歉让你久等了,看来你没有代码错误。 您需要做的最后一件事是,检查写入权限添加此代码块: 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);
}
}
答案 3 :(得分:0)
而不是使用它: -
File appDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + date + "/" + proj + "/" + photog);
试试这个: -
File appDirectory = new File(Environment.getExternalStorageDirectory() + "/" + date + "/" + proj ,photog );
答案 4 :(得分:0)
我刚修改了你的代码,这里是完整的代码
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);
和你的清单文件应该是:
<?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>
答案 5 :(得分:0)
你必须导入;;当你复制并粘贴它时,它不会自动导入。确保你有类似的导入:
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