package com.jservoire.hellomiss;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends Activity
{
	private ImageView image;
	private MainActivity context;
	private Toast loading;
	private ProgressBar loader;
	private Intent imgService;
	private Bitmap imageLoaded;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		context = this;
		this.image = (ImageView)findViewById(R.id.mainImage);
		this.loader = (ProgressBar)findViewById(R.id.loader);
		IntentFilter filter = new IntentFilter("downloadFinished");
		LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,filter);
    	loading = Toast.makeText(context, "Chargement...",Toast.LENGTH_LONG);
    	loading.show();
    	imgService = new Intent(MainActivity.this, ImageService.class);
    	loader.setVisibility(View.VISIBLE);
		startService(imgService);
	}
	
	private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() 
	{
		  @Override
		  public void onReceive(Context context, Intent intent) 
		  {
			  loading.cancel();
			  loader.setVisibility(View.INVISIBLE);
			  File imgFile = new File(Environment.getExternalStorageDirectory()+"/HelloMiss/newMiss.jpg");
			  if ( imgFile.exists() )
			  {
			      imageLoaded = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
			      image.setImageBitmap(imageLoaded);
			      imgFile.delete();
			  }		  
		  }
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected (MenuItem item)
	{
		switch( item.getItemId() )
		{
			case R.id.action_settings:
				Intent intent = new Intent(this, SettingActivity.class);
				startActivity(intent);
				return true;
			case R.id.refresh:
		    	loading = Toast.makeText(context, "Chargement...",Toast.LENGTH_LONG);
		    	loading.show();
		    	startService(imgService);
				return true;
			case R.id.idSaveImage:
				this.saveImage();
				return true;	
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	public void saveImage()
	{
	    File savePath = new File(Environment.getExternalStorageDirectory()+"/HelloMiss/");
	    if ( savePath != null )
	    {
	    	Calendar c = Calendar.getInstance();
	    	SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
	    	String nameFile = df.format(c.getTime())+".jpg";	    	
	    	File imgFile = new File(Environment.getExternalStorageDirectory()+"/HelloMiss/newMiss.jpg");
	    	File newImg = new File(savePath.getAbsolutePath(),nameFile);

			try 
			{
				InputStream in = new FileInputStream(imgFile);
				OutputStream out = new FileOutputStream(newImg);
				
		        // Transfer bytes from in to out
		        byte[] buf = new byte[1024];
		        int len;
		        while ((len = in.read(buf)) > 0) {
		            out.write(buf, 0, len);
		        }
		        in.close();
		        out.close();		        
		    	loading = Toast.makeText(context, "Image sauvegarde !",Toast.LENGTH_SHORT);
		    	loading.show();
			} 
			catch (FileNotFoundException e) {
				e.printStackTrace();
			} 
			catch (IOException e) {
				e.printStackTrace();
			}
	    }
	}
	
	@Override
	public void onResume(){
	    super.onResume();
	}
}

@@ -3244,17 +3244,16 @@
 t(%22dd-MM
-M
 -yyyy%22);
@@ -3318,107 +3318,8 @@
   %09%0A
-%09    %09File imgFile = new File(Environment.getExternalStorageDirectory()+%22/HelloMiss/newMiss.jpg%22);%0A
 %09   
@@ -3400,62 +3400,24 @@
 %09%7B%0A%09
-%09%09%09InputStream in = new FileInputStream(imgFile);%0A%09%09%09%09
+            File
 Outp
@@ -3425,19 +3425,17 @@
 tStream 
-out
+f
  = new F
@@ -3463,14 +3463,8 @@
 g);%0A
-%09%09%09%09%0A%09
 %09   
@@ -3472,248 +3472,68 @@
     
-// Transfer bytes from in to out%0A%09%09        byte%5B%5D buf = new byte%5B1024%5D;%0A%09%09        int len;%0A%09%09        while ((len = in.read(buf)) %3E 0) %7B%0A%09%09            out.write(buf, 0, len);%0A%09%09        %7D%0A%09%09        in.close();%0A%09%09        out.close();%09%09        
+    imageLoaded.compress(Bitmap.CompressFormat.JPEG, 85, f);
 %0A%09%09 
@@ -3707,66 +3707,8 @@
 ();%0A
-%09%09%09%7D %0A%09%09%09catch (IOException e) %7B%0A%09%09%09%09e.printStackTrace();%0A
 %09%09%09%7D

Change image name on save and fix save image.