Read and write images and text files Android

Although I REALLY appreciate this, and would have NEVER figured it out without you, there’s 2 small problems. One is a glitch. In your void saveMyImage(). My file will NOT save if the folder ALREADY EXISTS. If the folder DOES NOT exist then the program will make the folder and save the file normally. I highlighted the problems in the code here:

void saveMyImage(String sf, String tf){
  try{
    String absolute_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath());
    
    // Here's the first problem. This isn't a file. It's a folder. 
    // so it should be named either
    // File folder = new File(absolute_path+"/"+sf);
    // or
    // File directory = new File(absolute_path+"/"+sf);
    File file = new File(absolute_path+"/"+sf);
    println(file);
    
    //Here's the big problem. Since it's
    //a folder, this checks if the FOLDER
    //exists, not the file. So if the 
    //folder MyImageFolder exists then the
    //file will NOT be saved. It will only
    //save the file if the folder does NOT
    //exist. 
    if (!file.exists()){
      boolean success = true;
      success = file.mkdirs();
      if(success){
        save(file+"/"+tf);
        image_saved = true;
        println("File saved successfully.");
        println("Path",absolute_path,"File",file);
      }else{
        println("no File");
        println("Path",absolute_path,"File",file);
      }
    }
    
    //after this, there's nothing called
    //if(file.exists()) to write the file
    //if the folder exists. 
    
    
  }catch (Exception e){
    println("Error while saving file: " + e);
  }
};

That and I fixed the problems here:

void saveMyImage(String sf, String tf){
  try{
    String absolute_path = new String(Environment.getExternalStorageDirectory().getAbsolutePath());
    File directory = new File(absolute_path+"/"+sf);
    println(directory +" printlning file");
    
    if(!directory.exists()){
      println("directory.exists() = false");
      boolean MadeDirectory = true;
      MadeDirectory = directory.mkdirs();
      if(MadeDirectory){
        println("successfully made folder or directory to save file in");
        save(directory+"/"+tf);
        File file = new File(absolute_path+"/"+sf+"/"+tf);
        if(file.exists()){
          image_saved = true;
          println("File saved successfully.");
          println("Path",absolute_path,"directory",directory);
        }
        if(!file.exists()){
          image_saved = false;
          println("error: although folder was created, file could not be saved.");
          println("Path",absolute_path,"directory",directory);
        }
      }
      if(!MadeDirectory){
        println("error: could not create folder or directory");
        println("Path",absolute_path,"directory",directory);
      }
    }
    
    if(directory.exists()){
      println("directory.exists() = true");
      
      println("folder to save file in already exists. Attempting to save file...");
        save(directory+"/"+tf);
        File file = new File(absolute_path+"/"+sf+"/"+tf);
        if(file.exists()){
          image_saved = true;
          println("File saved successfully.");
          println("Path",absolute_path,"directory",directory);
        }
        if(!file.exists()){
          image_saved = false;
          println("error: although folder exists, file could not be saved.");
          println("Path",absolute_path,"directory",directory);
        }
      
      
    }
    
  }catch(Exception e){
    println("Error while saving file: " + e);
    println("could not make file or folder.");
    println("perhaps the permission was");
    println("not granted? Maybe try");
    println("uninstalling and re-installing.");
  }
}; 

But I REALLY thank you for showing me what you did! This was really good! I learned a lot!!!

1 Like