File plan_File = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + create_file_title.getText().toString() + ".txt");
FileOutputStream fs;
try {
fs = openFileOutput(create_file_title.getText().toString(), Context.MODE_PRIVATE);
fs.write(create_file_content.getText().toString().getBytes());
fs.close();
} } catch (Exception e) {
e.printStackTrace();
}
I only thought about googling and making a file, but I don't think about making a file even if I give the authority to mainfest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I'm the first one to miss something or do something wrong, but I don't know what's wrong.
File plan_folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "/" + main_folder + folder_name.getText().toString());
if (!plan_folder.exists()) {
plan_folder.mkdir();
}
File creation, permissions, and folder creation from the top, but neither file nor folder is created.
It's created when you press a button.
Below is the full code.
MainActivity
public class MainActivity extends AppCompatActivity {
Context context;
ScrollView scroll; // Main Screen Scroll View
Button btnAdd; // Add Main Screen Button
LinearLayout layout01;
EditText folder_name; // the title of the folder to create
View dialogView; // Dialog
final String[] versionArray = new String[] { "Add Schedule", "Add Folder"}; // Additional Dialog Items
AlertDialog.Builder dlgAdd; // Additional Dialogs
File main_folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "planner");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle ("Planner");
scroll = findViewById(R.id.scroll);
btnAdd = findViewById(R.id.btnAdd);
layout01 = findViewById(R.id.Mainlayout);
dlgAdd = new AlertDialog.Builder(MainActivity.this);
dlgAdd.setTitle ("add");
dlgAdd.setIcon(R.mipmap.ic_launcher);
dlgAdd.setItems(versionArray, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
if (which == 0) {
Intent intent = new Intent(getApplicationContext(), create_file_Activity.class);
startActivity(intent);
}
else {
dialogView = (View) View.inflate(MainActivity.this, R.layout.add_folder_dialog, null);
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setTitle ("Create Folder");
dlg.setView(dialogView);
dlg.setPositiveButton("OK", newDialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
folder_name = (EditText) dialogView.findViewById(R.id.folder_name);
File plan_folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "/" + main_folder + folder_name.getText().toString());
if (!plan_folder.exists()) {
plan_folder.mkdir();
}
if (!plan_folder.mkdirs()) {
Log.e("FILE", "Directory not created");
} } else {
Toast.makeText(getApplicationContext(), "Create Folder SUCCESS", Toast.LENGTH_SHORT).show();
}
if (folder_name == null) {
Toast.makeText(getApplicationContext(), "Please enter a folder name," Toast.LENGTH_SHORT).show();
}
}
});
dlg.setNegativeButton ("Cancel", null);
dlg.show();
};
}
});
dlgAdd.setPositiveButton("Close", null")
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dlgAdd.show();
}
});
}
// Listeners not working.
@Override // Options Menu
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add (0, 1, 0, "add");
menu.add (0, 2, 0, "delete");
menu.add (0, 3, 0, "settings");
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case 1:
dlgAdd.show();
return true;
case 2:
return true;
case 3:
return true;
}
return false;
}
}
create_file_Activity
public class create_file_Activity extends AppCompatActivity {
Context context;
LinearLayout main_layout;
EditText create_file_title, create_file_content;
Button create_file_btn_date, create_file_btn_time, create_file_btn_cancle, create_file_btn_confirm;
TextView time_tv, date_tv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_file);
setTitle ("Add Schedule");
create_file_title = (EditText) findViewById(R.id.create_file_title);
create_file_content = (EditText) findViewById(R.id.create_file_content);
create_file_btn_date = (Button) findViewById(R.id.create_file_btn_date);
create_file_btn_time = (Button) findViewById(R.id.create_file_btn_time);
create_file_btn_cancle = (Button) findViewById(R.id.create_file_btn_cancle);
create_file_btn_confirm = (Button) findViewById(R.id.create_file_btn_confirm);
main_layout = (LinearLayout) findViewById(R.id.Mainlayout);
time_tv = (TextView) findViewById(R.id.time_tv);
date_tv = (TextView) findViewById(R.id.date_tv);
context = this;
create_file_btn_cancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
create_file_btn_confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// CheckBox cbx = new CheckBox (context); // used to delete or perform additional functions on multiple files
// // cbx.setVisibility(View.INVISIBLE);
Button btn = new Button(context);
btn.setText(create_file_title.getText().toString());
btn.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
//main_layout.addView(cbx);
main_layout.addView(btn);
int i = 1;
// // btn.setId("@+id/plan_btn" + i); id register each
File plan_File = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + create_file_title.getText().toString() + ".txt");
FileOutputStream fs;
try {
fs = openFileOutput(create_file_title.getText().toString(), Context.MODE_PRIVATE);
fs.write(create_file_content.getText().toString().getBytes());
fs.close();
} } catch (Exception e) {
e.printStackTrace();
}
}
});
create_file_btn_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar now = Calendar.getInstance();
final TimePickerDialog tpDialog = new TimePickerDialog(create_file_Activity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
String state = "AM";
if (selectedHour > 12) {
selectedHour -= 12;
state = "PM";
}
time_tv.setText (state + " " + selectedHour + "hour" + selectedMinute + "minute"));
}
}, now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), false); // TimePicker appears in 24-hour format for true
tpDialog.getCurrentFocus();
tpDialog.show();
}
});
create_file_btn_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePickerDialog dpDialog = new DatePickerDialog(create_file_Activity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
date_tv.setText(i+"year"+(i1+1)+"month"+i2+"day"));
}
}, 0, 0, 0);
dpDialog.getDatePicker().setMinDate(System.currentTimeMillis());
dpDialog.show();
}
});
}
}
From Android 6.0 and above, you need to check the authority. https://developer.android.com/training/permissions/requesting?hl=ko
See here.
© 2024 OneMinuteCode. All rights reserved.