package com.example.fichier01;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
public class SecondActivity extends Activity {
    
	static TextView textView;
    static  EditText txtData;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        textView = (TextView) findViewById(R.id.text);
        // get the intent from which this activity is called.
         Intent intent = getIntent();
   
                // fetch value from key-value pair and make it visible on TextView.
        String item = intent.getStringExtra("selected-item");
        String ext = "";
        ext = item.substring(item.lastIndexOf('.'),item.length());
        
        
        if(ext.equals(".jpg"))
        {
        		ImageView image = (ImageView) findViewById(R.id.imageView);
        		
        		FileInputStream in;
        		BufferedInputStream buf;
		        try {
		       	    in = new FileInputStream("/sdcard/"+item);
		            buf = new BufferedInputStream(in);
		            Bitmap bMap = BitmapFactory.decodeStream(buf);
		            image.setImageBitmap(bMap);
		            if (in != null) 
		            {
		            	in.close();
		            }
		            if (buf != null) 
		            {
		            	buf.close();
		            }
		        } 
		        
		        catch (Exception e) 
		        {
		        	Log.e("Error reading file", e.toString());
		        }
        }
        
        else
        {
        	txtData = (EditText) findViewById(R.id.txtData);
            txtData.setHint("Enter some lines of data here...");
        	
        	
        	try {
                	File myFile = new File("/sdcard/"+item);
                	FileInputStream fIn = new FileInputStream(myFile);
                	BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
                
                	String aDataRow = "";
                	String aBuffer = "";
                	
                	while ((aDataRow = myReader.readLine()) != null) 
                	{
                		aBuffer += aDataRow + "\n";
                	}
                
                	txtData.setText(aBuffer);
                	myReader.close();
        	} 
        	
        	catch (Exception e)
        	{
                Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        	
        }
        
        textView.setText(ext);
        
    }
 
    
 
}
