0

Hi I have a a collection of json files in my assets/data/ folder on launch i want to check to see if the files exists in the internal files directory if the any of them dont exist i want to copy them from the assets/data/ folder to the internal storage files directory. At the moment i'm detecting if the files exist or not but struggling to copy them from assets. Does anyone know how i could achieve this?

heres what i have tried so far

public static void loadData(Context context){

        keyData[] = {"1","2","3","4","5","6"}
    JSONArray exists = new JSONArray();
    Log.v("Data", "keyData = " + keyData);
    Log.v("Data", "keyData.length = " + keyData.length);

        for (String directory : keyData ) {
              File file = new File(directory);
               if(file.exists() && file.isDirectory()){
                   exists.put("true");
               } else {
                   exists.put("false");
               }
        }

    Log.v("Data", "exists = " + exists);
    if(exists.toString().contains("false")){

        for(int i=0; i < keyData.length; i++){
            String filename = keyData[i];
            copyfileFromAssetsToInternalStorage(context,filename);
        }   
    }
} 




 public static void copyfileFromAssetsToInternalStorage(Context context,String filename){

     String DestinationFile = context.getFilesDir().getPath() + File.separator + filename;
     if (!new File(DestinationFile).exists()) {
       try {
         CopyFromAssetsToStorage(context, "data/" + filename, DestinationFile);
       } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
 }  
     private static void CopyFromAssetsToStorage(Context context, String SourceFile, String DestinationFile) throws IOException {
       InputStream IS = context.getAssets().open(SourceFile);
       OutputStream OS = new FileOutputStream(DestinationFile);
       CopyStream(IS, OS);
       OS.flush();
       OS.close();
       IS.close();
     }
     private static void CopyStream(InputStream Input, OutputStream output) throws IOException {
       byte[] buffer = new byte[5120];
       int length = Input.read(buffer);
       while (length > 0) {
         output.write(buffer, 0, length);
         length = Input.read(buffer);
       }
     }


 public final static void writeDataToFile(Context activityContext, String writableString, String fileName){

        FileOutputStream fos=null;
        try {
            fos=activityContext.openFileOutput(fileName, 0);
            activityContext.getFilesDir();
            fos.write(writableString.getBytes());


        } catch (FileNotFoundException e) {
             Log.e("CreateFile", e.getLocalizedMessage());
        }
        catch (IOException e) {
             Log.e("CreateFile", e.getLocalizedMessage());
        }

        finally{
            if(fos!=null){
                try {
                    // drain the stream
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    }
Luke Batley
  • 2,384
  • 10
  • 44
  • 76
  • Please explain, completely and precisely, what "struggling to copy them from assets" means. There are countless code examples for this, such as https://stackoverflow.com/a/4530294/115145 – CommonsWare Mar 24 '14 at 19:16
  • http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard – Nathaniel D. Waggoner Mar 24 '14 at 19:18

0 Answers0