package eu.redray.trevie.utility;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * Created by frano on 23/01/2016.
 */
public class PosterHeper {
    private final static String FILE_EXTENSION = ".jpg";
    private final static String DIRECTORY = "Posters";

    /**
     * Saves poster image to internal storage
     * @param context Application's context
     * @param bitmap Movie poster to save
     * @param title Movie's title
     * @return Path to saved file
     */
    public static String savePoster(Context context, Bitmap bitmap, String title) {
        OutputStream fOut = null;
        Uri outputFileUri = null;
        try {
            File root = new File(context.getFilesDir()
                    + File.separator + DIRECTORY + File.separator);
            root.mkdirs();
            File sdImageMainDirectory = new File(root, title + FILE_EXTENSION);
            outputFileUri = Uri.fromFile(sdImageMainDirectory);
            fOut = new FileOutputStream(sdImageMainDirectory);
        } catch (Exception e) {
            Log.e(PosterHeper.class.getSimpleName(), e.getMessage(), e);
            e.printStackTrace();
        }

        try {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
        } catch (Exception e) {
            Log.e(PosterHeper.class.getSimpleName(), e.getMessage(), e);
            e.printStackTrace();
        }

        return outputFileUri.toString();
    }

    public static boolean deletePoster(Context context, String title) {
        File file = new File(context.getFilesDir() + File.separator + DIRECTORY + File.separator,
                title + FILE_EXTENSION);
        return file.delete();
    }
}

@@ -1,8 +1,52 @@
+/*%0A * Copyright (C) 2016 Mateusz Widuch%0A */%0A
 package 
@@ -207,16 +207,54 @@
 o.File;%0A
+import java.io.FileNotFoundException;%0A
 import j
@@ -278,16 +278,44 @@
 Stream;%0A
+import java.io.IOException;%0A
 import j
@@ -347,39 +347,72 @@
 %0A * 
-Created by frano on 23/01/2016.
+Provides methods for retrieving and deleting movie poster images
 %0A */
@@ -494,10 +494,10 @@
 = %22.
-j
 p
+n
 g%22;%0A
@@ -606,16 +606,23 @@
 storage%0A
+     *%0A
      * @
@@ -639,29 +639,60 @@
 ext 
-Application's context
+the context of the app giving access to file storage
 %0A   
@@ -709,20 +709,19 @@
  bitmap 
-Movi
+ th
 e poster
@@ -725,48 +725,79 @@
 ter 
-to save%0A     * @param title Movie's titl
+of a movie provided as bitmap%0A     * @param title   the title of a movi
 e%0A  
@@ -809,17 +809,28 @@
 @return 
-P
+       the p
 ath to s
@@ -834,19 +834,20 @@
 o saved 
-fil
+imag
 e%0A     *
@@ -1153,22 +1153,188 @@
     
-root.mkdirs();
+// Create root directory if it doesn't exist%0A            if (!root.exists()) %7B%0A                //noinspection ResultOfMethodCallIgnored%0A                root.mkdirs();%0A            %7D
 %0A   
@@ -1546,32 +1546,44 @@
        %7D catch (
+FileNotFound
 Exception e) %7B%0A 
@@ -1688,34 +1688,47 @@
 ace();%0A        %7D
-%0A%0A
+ finally %7B%0A    
         try %7B%0A  
@@ -1717,32 +1717,36 @@
           try %7B%0A
+    
             bitm
@@ -1812,22 +1812,40 @@
     
-fOut.flush();%0A
+    if (fOut != null) %7B%0A        
     
@@ -1862,24 +1862,46 @@
 ut.close();%0A
+                %7D%0A    
         %7D ca
@@ -1905,16 +1905,18 @@
  catch (
+IO
 Exceptio
@@ -1914,32 +1914,36 @@
 OException e) %7B%0A
+    
             Log.
@@ -1991,32 +1991,36 @@
 tMessage(), e);%0A
+    
             e.pr
@@ -2028,32 +2028,46 @@
 ntStackTrace();%0A
+            %7D%0A
         %7D%0A%0A     
@@ -2105,24 +2105,256 @@
 g();%0A    %7D%0A%0A
+    /**%0A     * Deletes poster image from internal storage%0A     *%0A     * @param context the context of the app giving access to file storage%0A     * @param title   the title of a movie%0A     * @return        true if successful%0A     */%0A
     public s

Improve exception handling in PosterHelper class