AndroidStudio unable to save a file on the Emulator
I would like to save/create a file in the directory of my Emulator but i cant do that and i dont understand what is wrong with my code. So everytime i try to run the code it says that android cant create the file in such directory.
Can someone please explain to me how i make it. Here is the code of my Application:
public class MainActivity extends AppCompatActivity {
private int STORAGE_PERMISSION_CODE = 1;
private File csvFile;
SimpleDateFormat TIME;
private static final String CSV_DIRECTORY = "NameOfTheDirectory";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if((ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)){
Toast.makeText(MainActivity.this, "You have already granted this permission",
Toast.LENGTH_SHORT).show();
}else {
requestStoragePermission();
}
}
public void makeFile(View view) {
File csvDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY);
if(!csvDirectory.exists()) {
try{
csvDirectory.mkdir();
Log.d("StateMakeFile","Directory created");
} catch(Exception e) {
e.printStackTrace();
Log.d("StateMakeFile","Directory not created");
}
}
File categoryDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY + File.separator + "NameOfTheCategory");
if(!categoryDirectory.exists()){
try{
categoryDirectory.mkdir();
Log.d("StateMakeFile","CategoryDirectory created");
}catch (Exception e){
e.printStackTrace();
Log.d("StateMakeFile","CategoryDirectory not created");
}
}
TIME = new SimpleDateFormat("yyyyMMdd-hh:mm:ss:SSS", Locale.getDefault());
String uniqueFileName = TIME.format(new Date());
csvFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY + File.separator + "NameOfTheCategory" +
File.separator + uniqueFileName + ".csv");
if(!csvFile.exists()){
try{
csvFile.createNewFile();
Log.d("StateMakeFile","File created");
}catch (IOException e){
e.printStackTrace();
Log.d("StateMakeFile","File not created");
}
if(csvFile.exists()) {
try{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(csvFile, true)));
out.write("Something" + "n");
out.flush();
out.close();
Log.d("StateMakeFile","File was writed with success");
}catch (IOException e){
e.printStackTrace();
Log.d("StateMakeFile","File wasnt writed with success");
}
}
}
}
private void requestStoragePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(this)
.setTitle("Permission needed")
.setMessage("This permission is needed to save and load files into ssd")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,
new String {Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create().show();
} else {
ActivityCompat.requestPermissions(this,
new String {Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
}
}
}
}
java android file filewriter
add a comment |
I would like to save/create a file in the directory of my Emulator but i cant do that and i dont understand what is wrong with my code. So everytime i try to run the code it says that android cant create the file in such directory.
Can someone please explain to me how i make it. Here is the code of my Application:
public class MainActivity extends AppCompatActivity {
private int STORAGE_PERMISSION_CODE = 1;
private File csvFile;
SimpleDateFormat TIME;
private static final String CSV_DIRECTORY = "NameOfTheDirectory";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if((ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)){
Toast.makeText(MainActivity.this, "You have already granted this permission",
Toast.LENGTH_SHORT).show();
}else {
requestStoragePermission();
}
}
public void makeFile(View view) {
File csvDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY);
if(!csvDirectory.exists()) {
try{
csvDirectory.mkdir();
Log.d("StateMakeFile","Directory created");
} catch(Exception e) {
e.printStackTrace();
Log.d("StateMakeFile","Directory not created");
}
}
File categoryDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY + File.separator + "NameOfTheCategory");
if(!categoryDirectory.exists()){
try{
categoryDirectory.mkdir();
Log.d("StateMakeFile","CategoryDirectory created");
}catch (Exception e){
e.printStackTrace();
Log.d("StateMakeFile","CategoryDirectory not created");
}
}
TIME = new SimpleDateFormat("yyyyMMdd-hh:mm:ss:SSS", Locale.getDefault());
String uniqueFileName = TIME.format(new Date());
csvFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY + File.separator + "NameOfTheCategory" +
File.separator + uniqueFileName + ".csv");
if(!csvFile.exists()){
try{
csvFile.createNewFile();
Log.d("StateMakeFile","File created");
}catch (IOException e){
e.printStackTrace();
Log.d("StateMakeFile","File not created");
}
if(csvFile.exists()) {
try{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(csvFile, true)));
out.write("Something" + "n");
out.flush();
out.close();
Log.d("StateMakeFile","File was writed with success");
}catch (IOException e){
e.printStackTrace();
Log.d("StateMakeFile","File wasnt writed with success");
}
}
}
}
private void requestStoragePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(this)
.setTitle("Permission needed")
.setMessage("This permission is needed to save and load files into ssd")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,
new String {Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create().show();
} else {
ActivityCompat.requestPermissions(this,
new String {Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
}
}
}
}
java android file filewriter
add a comment |
I would like to save/create a file in the directory of my Emulator but i cant do that and i dont understand what is wrong with my code. So everytime i try to run the code it says that android cant create the file in such directory.
Can someone please explain to me how i make it. Here is the code of my Application:
public class MainActivity extends AppCompatActivity {
private int STORAGE_PERMISSION_CODE = 1;
private File csvFile;
SimpleDateFormat TIME;
private static final String CSV_DIRECTORY = "NameOfTheDirectory";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if((ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)){
Toast.makeText(MainActivity.this, "You have already granted this permission",
Toast.LENGTH_SHORT).show();
}else {
requestStoragePermission();
}
}
public void makeFile(View view) {
File csvDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY);
if(!csvDirectory.exists()) {
try{
csvDirectory.mkdir();
Log.d("StateMakeFile","Directory created");
} catch(Exception e) {
e.printStackTrace();
Log.d("StateMakeFile","Directory not created");
}
}
File categoryDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY + File.separator + "NameOfTheCategory");
if(!categoryDirectory.exists()){
try{
categoryDirectory.mkdir();
Log.d("StateMakeFile","CategoryDirectory created");
}catch (Exception e){
e.printStackTrace();
Log.d("StateMakeFile","CategoryDirectory not created");
}
}
TIME = new SimpleDateFormat("yyyyMMdd-hh:mm:ss:SSS", Locale.getDefault());
String uniqueFileName = TIME.format(new Date());
csvFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY + File.separator + "NameOfTheCategory" +
File.separator + uniqueFileName + ".csv");
if(!csvFile.exists()){
try{
csvFile.createNewFile();
Log.d("StateMakeFile","File created");
}catch (IOException e){
e.printStackTrace();
Log.d("StateMakeFile","File not created");
}
if(csvFile.exists()) {
try{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(csvFile, true)));
out.write("Something" + "n");
out.flush();
out.close();
Log.d("StateMakeFile","File was writed with success");
}catch (IOException e){
e.printStackTrace();
Log.d("StateMakeFile","File wasnt writed with success");
}
}
}
}
private void requestStoragePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(this)
.setTitle("Permission needed")
.setMessage("This permission is needed to save and load files into ssd")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,
new String {Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create().show();
} else {
ActivityCompat.requestPermissions(this,
new String {Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
}
}
}
}
java android file filewriter
I would like to save/create a file in the directory of my Emulator but i cant do that and i dont understand what is wrong with my code. So everytime i try to run the code it says that android cant create the file in such directory.
Can someone please explain to me how i make it. Here is the code of my Application:
public class MainActivity extends AppCompatActivity {
private int STORAGE_PERMISSION_CODE = 1;
private File csvFile;
SimpleDateFormat TIME;
private static final String CSV_DIRECTORY = "NameOfTheDirectory";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if((ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)){
Toast.makeText(MainActivity.this, "You have already granted this permission",
Toast.LENGTH_SHORT).show();
}else {
requestStoragePermission();
}
}
public void makeFile(View view) {
File csvDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY);
if(!csvDirectory.exists()) {
try{
csvDirectory.mkdir();
Log.d("StateMakeFile","Directory created");
} catch(Exception e) {
e.printStackTrace();
Log.d("StateMakeFile","Directory not created");
}
}
File categoryDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY + File.separator + "NameOfTheCategory");
if(!categoryDirectory.exists()){
try{
categoryDirectory.mkdir();
Log.d("StateMakeFile","CategoryDirectory created");
}catch (Exception e){
e.printStackTrace();
Log.d("StateMakeFile","CategoryDirectory not created");
}
}
TIME = new SimpleDateFormat("yyyyMMdd-hh:mm:ss:SSS", Locale.getDefault());
String uniqueFileName = TIME.format(new Date());
csvFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY + File.separator + "NameOfTheCategory" +
File.separator + uniqueFileName + ".csv");
if(!csvFile.exists()){
try{
csvFile.createNewFile();
Log.d("StateMakeFile","File created");
}catch (IOException e){
e.printStackTrace();
Log.d("StateMakeFile","File not created");
}
if(csvFile.exists()) {
try{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(csvFile, true)));
out.write("Something" + "n");
out.flush();
out.close();
Log.d("StateMakeFile","File was writed with success");
}catch (IOException e){
e.printStackTrace();
Log.d("StateMakeFile","File wasnt writed with success");
}
}
}
}
private void requestStoragePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(this)
.setTitle("Permission needed")
.setMessage("This permission is needed to save and load files into ssd")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,
new String {Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create().show();
} else {
ActivityCompat.requestPermissions(this,
new String {Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) {
if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
}
}
}
}
java android file filewriter
java android file filewriter
asked Nov 24 '18 at 18:36
Marco SchwarzMarco Schwarz
114
114
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I don‘t know if it is in your manifest, but your are working here with
READ_EXTERNAL_STORAGE
Try it with the permission
WRITE_EXTERNAL_STORAGE
Hope this helps you
Ohhh true i totaly didnt see it, thanks ill try now i hope will work now
– Marco Schwarz
Nov 24 '18 at 19:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461234%2fandroidstudio-unable-to-save-a-file-on-the-emulator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don‘t know if it is in your manifest, but your are working here with
READ_EXTERNAL_STORAGE
Try it with the permission
WRITE_EXTERNAL_STORAGE
Hope this helps you
Ohhh true i totaly didnt see it, thanks ill try now i hope will work now
– Marco Schwarz
Nov 24 '18 at 19:01
add a comment |
I don‘t know if it is in your manifest, but your are working here with
READ_EXTERNAL_STORAGE
Try it with the permission
WRITE_EXTERNAL_STORAGE
Hope this helps you
Ohhh true i totaly didnt see it, thanks ill try now i hope will work now
– Marco Schwarz
Nov 24 '18 at 19:01
add a comment |
I don‘t know if it is in your manifest, but your are working here with
READ_EXTERNAL_STORAGE
Try it with the permission
WRITE_EXTERNAL_STORAGE
Hope this helps you
I don‘t know if it is in your manifest, but your are working here with
READ_EXTERNAL_STORAGE
Try it with the permission
WRITE_EXTERNAL_STORAGE
Hope this helps you
answered Nov 24 '18 at 18:45
Alexander DauerAlexander Dauer
266
266
Ohhh true i totaly didnt see it, thanks ill try now i hope will work now
– Marco Schwarz
Nov 24 '18 at 19:01
add a comment |
Ohhh true i totaly didnt see it, thanks ill try now i hope will work now
– Marco Schwarz
Nov 24 '18 at 19:01
Ohhh true i totaly didnt see it, thanks ill try now i hope will work now
– Marco Schwarz
Nov 24 '18 at 19:01
Ohhh true i totaly didnt see it, thanks ill try now i hope will work now
– Marco Schwarz
Nov 24 '18 at 19:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53461234%2fandroidstudio-unable-to-save-a-file-on-the-emulator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown