package com.example.uri02;


import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.provider.MediaStore.MediaColumns;

import java.io.File;
import java.net.URI;
import java.text.DateFormat.Field;
import java.util.ArrayList;
import java.util.SortedSet;
import java.util.TreeSet;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

import java.lang.reflect.*;


public class MainActivity extends Activity {

   public void onCreate(Bundle savedInstanceState) 
   {
	   		super.onCreate(savedInstanceState);
	   		setContentView(R.layout.activity_main);
	   		
	   		java.lang.reflect.Field[] declaredFields = (R.drawable.class).getDeclaredFields();
	   		
	   		ArrayList<String> list = new ArrayList<String>();
	   		String targetPath = "/storage/sdcard/patoche/";
	         
	        File targetDirector = new File(targetPath);
	         
	      
	        File[] files = targetDirector.listFiles();
	        for(File file : files){
	            list.add(file.getAbsolutePath());
	        }
	   		
	   		
	   		
	   		getImageFromSdCard("toto"); 
	   		
	   		int resID = getResources().getIdentifier("a", "drawable", getPackageName());
	   		
	   		Resources r = getResources();
	   		int drawableId = r.getIdentifier("drawableName", "drawable", "com.example.uri02");
	   		
	   		int resourceId;
	   		
	   		final R.drawable drawableResources = new R.drawable();
	   		final Class<R.drawable> cls = R.drawable.class;
	   		final java.lang.reflect.Field[] fields = cls.getDeclaredFields();
	   		
	   		for (int i = 0, max = fields.length; i < max; i++) {
	   		    
	   		    try {
	   		        resourceId = fields[i].getInt(drawableResources);
	   		    } catch (Exception e) {
	   		        continue;
	   		    }
	   		    /* make use of resourceId for accessing Drawables here */
	   		}
	   		
	   		
	 /*  		try {
	   		    Class res = R.drawable.class;
	   		    Field field = res.getField("drawableName");
	   		    int drawableId = field.getInt(null);
	   		}
	   		
	   /*		
	   		Field[] drawables = R.drawable.class.getFields();
	   		for (Field f : drawables) {
	   		    try {
	   		        System.out.println("R.drawable." + f.getName());
	   		    } catch (Exception e) {
	   		        e.printStackTrace();
	   		    }
	   		}
      */
/****** Essai 1 **********/     
//	   		Uri baseUri = Uri.parse("content://mnt/sdcard/");
//	   		String[] proj = { MediaStore.Images.Media.DATA };
//	   		Cursor cursor = getContentResolver().query(baseUri, proj, null, null, null);

/****** Essai 2 **********/     

//	   		File myFile = new File("/scard/patoche");
//	   		Uri myUri = Uri.fromFile(myFile);
//	   		Cursor mImageCursor = getContentResolver().query(myUri, proj, null, null, null );
	   		
/****** Essai 3 **********/     
	   		String folder = "/mnt/sdcard/patoche/";
	   		folder = folder + "%";
	   		String where = Media.DATA + " LIKE ?";
	   		String[] whereArgs = new String[]{folder};
	   		
	   		File afile = null;
	   		
	   		String[] selectionArgs = new String[] { afile.getAbsolutePath() };
	   		
	   		
	   		String[] p = {MediaStore.Images.ImageColumns.DATA}; 

	   		Cursor plouf = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
	   		    p, where, whereArgs, MediaColumns._ID);
	   		
	   		String aaa = "dd";
	   		
	   		Uri uri = null;
	   		
	   		uri = Uri.parse(folder);
	   		
	   		
	   		folder = "/mnt/sdcard/patoche/";
	   		folder = folder + "%";
	   		whereArgs = new String[]{folder};
	   	//	Cursor plouf = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI , p, null, whereArgs, null); 
	   		
	   		int res = plouf.getCount();
	   		
/****** Essai 4 **********/     

	   		
	   		SortedSet<String> dirList = new TreeSet<String>();
	   		String[] directories = null; 
	   		
	   		Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
	        String[] projection = {MediaStore.Images.ImageColumns.DATA}; 
	        Cursor c = null;
	   		
	        //http://stackoverflow.com/questions/20185523/list-out-all-images-from-sd-card
	        
	        c = managedQuery(u, projection, null, null, null); 
	        
	        int n = c.getCount();
	        
	        if ((c != null) && (c.moveToFirst())) 
	        { 
	            do 
	            {
	                String tempDir = c.getString(0);
	                tempDir = tempDir.substring(0, tempDir.lastIndexOf("/"));
	                try{
	                    dirList.add(tempDir);
	                }
	                catch(Exception e)
	                {

	                }
	            } 
	            while (c.moveToNext());
	            directories = new String[dirList.size()];
	            dirList.toArray(directories);

	        }
	        
	        
	   		String titi = "Salut";
	   		
   }
   
   public Drawable getImageFromSdCard(String imageName) 
   {
	    Drawable d = null;
	    try {
	        	//String path = Environment.getExternalStorageDirectory().toString()    + "/YourSubDirectory/";
	        	String path = "/mnt/sdcard/patoche/b0008.jpg";
	        	//Bitmap bitmap = BitmapFactory.decodeFile(path + "/" + imageName    + ".png");
	        	Bitmap bitmap = BitmapFactory.decodeFile(path);

	        	d = new BitmapDrawable(bitmap);
	    } 
	    catch (IllegalArgumentException e)
	    {
	        // TODO: handle exception
	    }
	    return d;

	}
   
   
}