Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Data Persistence File Storage Writing to Internal Storage

Miguel Angel Rodriguez
Miguel Angel Rodriguez
11,280 Points

Cant fin the error! Please help

private static boolean copyFile(InputStream in, FileOutputStream out) throws IOException { // Copy magic intentionally omitted try { byte[] buffer = new byte[1024]; int read; while((read = in.read(buffer)) != -1){ out.write(buffer, 0, read); } } catch(FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return true; } }

FileUtilities.java
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileUtilities {

  public static boolean copyResult;

  public static void saveAssetImage(Context context, String assetName) throws IOException {
    File fileToWrite = new File(context.getFilesDir(), assetName);
    AssetManager assetManager = context.getAssets();

    try {
      InputStream in = assetManager.open(assetName);
      FileOutputStream out = new FileOutputStream(fileToWrite);
      copyResult = copyFile(in, out);
    } catch(FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  private static boolean copyFile(InputStream in, FileOutputStream out) throws IOException {
    // Copy magic intentionally omitted
    try {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
      } catch(FileNotFoundException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return true;
  }
}

Can you also comment the error that you are seeing?

2 Answers

Hello,

What you are currently trying to do is write the "copy magic" that "omitted" which you don't need to do for this challenge. It was just omitted from the screen, but is still present and called in the final product so it doesn't need to actually be written. Instead, you need to check if the output file exists in saveAssetImage and if it does, you want to do nothing, otherwise, you want to execute the try block in saveAssetImage.

Miguel Angel Rodriguez
Miguel Angel Rodriguez
11,280 Points

Hi, I tried with the method "exists" before the Try

public class FileUtilities {

  public static boolean copyResult;

  public static void saveAssetImage(Context context, String assetName) {
    File fileToWrite = new File(context.getFilesDir(), assetName);
    AssetManager assetManager = context.getAssets();
    if (fileToWrite.exists()) {
      try {
          InputStream in = assetManager.open(assetName);
          FileOutputStream out = new FileOutputStream(fileToWrite);
          copyResult = copyFile(in, out); 
      } catch(FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

The error said: "The file is not being copied. Identify the code that tries and add an "if" check around it. I tried with the if inside the "try" and nothing happens.

Thanks for help

You're currently copying only if the file exists, but you want to only write it if the file does not exist.