package io.smartlogic.smartchat.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

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

import io.smartlogic.smartchat.Constants;
import io.smartlogic.smartchat.R;
import io.smartlogic.smartchat.views.DrawingView;

public class SmartChatPreviewActivity extends Activity {
    private DrawingView mDrawingView;
    private Button mUploadButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_chat_preview);

        getActionBar().hide();

        File pictureFile = new File(getIntent().getExtras().getString(Constants.EXTRA_PHOTO_PATH));
        Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());

        ImageView preview = (ImageView) findViewById(R.id.smartchat);
        preview.setImageBitmap(bitmap);

        RelativeLayout layout = (RelativeLayout) findViewById(R.id.container);

        RelativeLayout.LayoutParams previewLayoutParams = (RelativeLayout.LayoutParams) preview.getLayoutParams();

        mDrawingView = new DrawingView(this);
        mDrawingView.setLayoutParams(previewLayoutParams);

        layout.addView(mDrawingView, 1);

        mUploadButton = (Button) findViewById(R.id.upload);
        mUploadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SmartChatPreviewActivity.this, UploadActivity.class);

                if (mDrawingView.doesDrawingExist()) {
                    String drawingPhotoPath = saveDrawing();
                    intent.putExtra(Constants.EXTRA_DRAWING_PATH, drawingPhotoPath);
                }

                intent.putExtra(Constants.EXTRA_PHOTO_PATH, getIntent().getExtras().getString(Constants.EXTRA_PHOTO_PATH));
                startActivity(intent);
            }
        });
    }

    private String saveDrawing() {
        try {
            File pictureFile = File.createTempFile("drawing", ".png", getExternalCacheDir());

            OutputStream out = new FileOutputStream(pictureFile);
            mDrawingView.getDrawingCache().compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();

            return pictureFile.getAbsolutePath();
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

@@ -615,24 +615,54 @@
  Activity %7B%0A
+    private File pictureFile;%0A
     private 
@@ -932,37 +932,32 @@
 ide();%0A%0A        
-File 
 pictureFile = ne
@@ -2268,24 +2268,141 @@
  %7D);%0A    %7D%0A%0A
+    @Override%0A    public void onBackPressed() %7B%0A        pictureFile.delete();%0A%0A        super.onBackPressed();%0A    %7D%0A%0A
     private 

Remove cached photo if the user hits back